[英]Global Context - Check for undefined
為什么以下代碼的結果為true
三倍?
我在第二步中排除了false
。
function foo() {
this.bar = function () { };
};
console.log("foo - defined : " + typeof window.foo !== 'undefined');
console.log("bar - defined : " + typeof window.bar !== 'undefined');
foo();
console.log("bar - defined : " + typeof window.bar !== 'undefined');
+
運算符的優先級高於!==
的優先級。 你的意思是
("bar - defined : " + typeof window.bar) !== 'undefined' // always true (or an exception)
代替
"bar - defined : " + (typeof window.bar !== 'undefined')
如果顯式地執行后者,則將獲得預期的輸出:
foo - defined : true
bar - defined : false
bar - defined : true
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.