なぜこれが起こるのか誰かが私に説明することができますか?そして、誰かがこの行動の名前を知っているなら、タイトルを編集してください。表現で奇妙な振る舞いJavaScript
const arr = ['RIPA'], varB = "RIPB";
let _params;
_params && Array.isArray(_params) ? arr.push(..._params) :
arr.push(_params);
_params && console.log("I will never appear");
varB && console.log("I will appear");
arr.push(varB);
console.log('array',arr);
console.log("Type of the _params --> ", typeof _params);
出力:このコードで
array [ 'RIPA', undefined, 'RIPB' ]
Type of the _params --> undefined
jsBIN: https://jsbin.com/bawepasivo/edit?js,console
repl.it: https://repl.it/GaHX
_params
があるのか、可能であればundefined
実行する&&
式が最初のfalseと最後の信頼できる値を返す場合、2番目の式を返します。
'arr.push(_params)'は 'arr'の' undefined'をプッシュしました – Mistalis
あなたの質問は正確には何か、それは少し不明です。 '_params && Array.isArray(_params)'は 'false'と評価され、' arr.push(_params); 'が呼び出され、' undefined'を 'arr'にプッシュします。 – Stijn
しかし、&&式は最初の偽値を返します。私の考えでは、コンパイラは式の2番目の部分を決して実行できません。 –