"Closure Compiler"を使用していますが、スクリプトをコンパイルするとき、次のことを行います。
コンパイルする前に:
// ==ClosureCompiler==
// @compilation_level SIMPLE_OPTIMIZATIONS
// @output_file_name default.js
// @formatting pretty_print,print_input_delimiter
// ==/ClosureCompiler==
var myObj1 = (function() {
var undefined; //<----- declare undefined
this.test = function(value, arg1) {
var exp = 0;
arg1 = arg1 == undefined ? true : arg1; //<----- use declare undefined
exp = (arg1) ? value * 5 : value * 10;
return exp;
};
return this;
}).call({});
var myObj2 = (function() {
this.test = function(value, arg1) {
var exp = 0;
arg1 = arg1 == undefined ? true : arg1; //<----- without declare undefined
exp = (arg1) ? value * 5 : value * 10;
return exp;
};
return this;
}).call({});
編集済み:
// Input 0
var myObj1 = function() {
this.test = function(b, a) {
a = a == void 0 ? true : a; //<-----
var c = 0;
return c = a ? b * 5 : b * 10
};
return this
}.call({}), myObj2 = function() {
this.test = function(b, a) {
a = a == undefined ? true : a; //<-----
var c = 0;
return c = a ? b * 5 : b * 10
};
return this
}.call({});
これにより、「void 0」と「undefined」の使用についての質問は、使用に違いがあるのでしょうか、それとも2つのケースは問題ないのでしょうか。
編集する
「void 0」でコンパイルされた「var undefined」を定義した場合、「undedined」でコンパイルされた「undefined」を定義しなかった場合、「undefined」と「void 0」の間の文字数は問題になりません。
編集II:このリンクに基づくパフォーマンス
IE 8:
typeof:228ms
undefined:62ms
void 0:57ms
Firefox 3.6:
typeof:10ms
未定義:3ms
void 0:3ms
Opera 11:
typeof:67ms
undefined:19ms
void 0:20ms
Chrome 8:
typeof:3ms
undefined:5ms
void 0:3ms
void
オペレータは、与えられた評価expression
し、次いで戻りますundefined
。この演算子を使用すると、未定義に評価される式が必要な場所に、副作用を生成する式を挿入できます。
多くの場合、void演算子は
undefined
プリミティブ値を取得するためにのみ使用され、通常は "void(0)
"(" "と同等void 0
)を使用します。このような場合は、undefined
代わりにグローバル変数を使用できます(デフォルト以外の値に割り当てられていない場合)。
閉鎖コンパイラはにスワップしvoid 0
、それはより少ない文字が含まれているためundefined
、そのため同等の、より小さなコードを生成します。
Re:OPコメント
はい、私はドキュメントを読みましたが、私が与えた例では、「ボイド0」と別の「未定義」を使用する場合の「グーグル閉鎖」
これは実際にはGoogle Closure Compilerのバグだと思います。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加