更新:
for( i in window)if(i=='onhashchange')console.log(i, window[i]); //prints onchangechange undefined
on browser where onhashchange event is supported I have
'onhashchange' in window; //returns true
window['onhashchange']; //returns false
window.onhashchange; //returns false;
为什么前者返回true,其余返回false?
最佳答案
会不会很简单,window
对象有一个名为onhashchange
的属性,但是该属性的值当前为null
,因此被认为假的?
var a = { 'foo': null }
console.log('foo' in a) // true
console.log(a.foo) // null
console.log(!!a.foo) // false
…其中 !!
是一个双重否定,一个将值转换为 true/false 的技巧。
关于运算符和事件中的 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4756437/