运算符和事件中的 javascript

标签 javascript

更新:

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/

相关文章:

javascript - setInterval 在 javascript 中遍历数组?

javascript - 需要。 JavaScript 3D 框架,具有帧速率和键盘事件的精确时间戳

javascript - 在Jquery中解析Json数据

javascript - 用命名函数替换匿名函数(在 jQuery 中)

javascript - 如何使用下划线创建自己的可继承类

javascript - js-routes ASP.NET MVC 替代方案

javascript - 使用 jquery 或 javascript 从 iframe 控制顶部父窗口

javascript - 无法在 React 应用程序中编辑输入

javascript - 如何将 HTML 内容设置到 iframe 中

javascript - 将数组值从 html 传递到 javascript