Pix*_*bot 0 javascript firefox google-chrome
假设我有一个对象,它有一个返回对象本身的方法.
var mystate = {
init: function() {
return this;
},
run: function() {
console.log("!");
}
}
Run Code Online (Sandbox Code Playgroud)
然后我运行这段代码:
var status = mystate.init();
mystate.run();
status.run();
Run Code Online (Sandbox Code Playgroud)
在Firefox中,这会打印两个"!" 控制台中的标志,但不知何故在Chrome中打印出第一个,但我收到错误,
未捕获的TypeError:对象[object Object]没有方法'run'
什么时候运行 status.run()
当我status.run()在Chrome中运行时,状态似乎未定义,但在Firefox中则不行.
那么......为什么会这样,我怎么能让一个物体返回呢?
你不能status在Chrome中设置这样的,因为你真的在设置window.status,它必须是一个字符串.
因此status成为String "[object Object]"(即将Object转换为String的结果),并且String实例没有调用的方法run,因此您会收到错误.