IntelliJ IDEA のJSコード添削メモ
配列の定義はnew Array() より []で
before
var tmp = new Array();
Array instantiation can be simplified.
Checks for improper usage of wrappers for JavaScript primitive types.
Also, warning will be produced when property of primitive type is modified, as assigned value will be lost.
after
var tmp = [];
for in ループでプロパティの存在チェックを
before
var current_id; for(var id in ids) { current_id = ids[id]; }
Possible iteration over unexpected (custom / inherited) members, probably missing hasOwnProperty check.
Checks for using unfiltered for-in loops in JavaScript, using this construction causes processing inherited or unexpected properties, one needs to filter own properties with hasOwnProperty() method. The validation works in JavaScript, html or jsp files.
after
var current_id; for(var id in ids) { if(ids.hasOwnProperty(id)) { var current_id = ids[id]; } }
jQuery IDセレクタの重複(同じIDセレクタが複数回登場し、毎回DOMを検索している)
before
// 移動先の要素をテンポラリにコピー for(var i=1; i<=3; i++) { tmp['td'+i] = $('#td'+i+'_'+moved_id).children().clone(); // 移動元の要素を移動先に移動 $('#td'+i+'_'+moved_id).children().replaceWith($('#td'+i+'_'+current_id).children()); // テンポラリの要素を移動元に移動 $('#td'+i+'_'+current_id).append(tmp['td'+i]); }
Duplicated jQuery selector.
Checks that jQuery selectors are used in an efficient way. It suggests to split descendant selectors which are prefaced with ID selector and warns about duplicated selectors which could be cached.
after
// 移動先の要素をテンポラリにコピー for(var i=1; i<=3; i++) { var moved_obj = $('#td'+i+'_'+moved_id); var current_obj = $('#td'+i+'_'+current_id); tmp['td'+i] = moved_obj.children().clone(); // 移動元の要素を移動先に移動 moved_obj.children().replaceWith(current_obj.children()); // テンポラリの要素を移動元に移動 current_obj.append(tmp['td'+i]); }
var 定義漏れ
変数がvarで宣言されていない。for in 時も。
before
for(i in ids) { current_id = i; }
Variable id implicitly declared.
Checks JavaScript variables to be declared explicitly with var statement. The validation works in JavaScript, html or jsp files.
after
var current_id; for(var i in ids) { current_id = i; }