FireFox& JavaScript中不同的JavaScript对象文字行为谷歌浏览器? [英] Different JavaScript object literal behaviour in FireFox & Google Chrome?

查看:222
本文介绍了FireFox& JavaScript中不同的JavaScript对象文字行为谷歌浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经缩小了我的错误,以遵循在谷歌浏览器中产生不同行为的代码集: http://jnvxxx.appspot.com/rpc/static_server?key_=testjs.html



Firefox输出:Hi 1 [object Object] 192 Hi 2



Chrome输出:Hi 1 [object Object] undefined Hi 2



任何想法都可以在google chrome中得到属性。



谢谢。


您正在访问 window.status 属性,该属性用于控制状态栏中的文本。请参阅: http://www.w3schools.com/jsref/prop_win_status.asp 。 / p>

显然,这个功能必须先在所有主流浏览器中打开,所以显然不同的浏览器在关闭时会做不同的事情。 Chrome将状态属性的值更改为 string ,所以它变成了隐藏但熟悉的字符串[object Object],它没有entry_count属性。 Firefox将对象保留在状态属性中。



底线: window.status 已被其他用途使用;为你的变量使用一个不同的名字。



编辑:

如下所述,所有这一切都将它封装在函数范围内,只要你不打算在其他地方使用它:

$ p $ (function(){
var myStatus = {...};
//用myStatus做一些事情,最好不要document.write;)
}());

var a = typeof myStatus; // a ==='undefined'。

这样,变量只能在函数范围内可见。 b

I have narrowed down my error to following set of codes producing different behaviour in google Chrome:

Sample Code: http://jnvxxx.appspot.com/rpc/static_server?key_=testjs.html

Firefox Output:Hi 1 [object Object]192 Hi 2

Chrome Output: Hi 1 [object Object]undefined Hi 2

Any idea hot to get attributes working in google chrome as well.

Thanks.

解决方案

You are accessing the window.status property, which is used to control the text in the status bar. See: http://www.w3schools.com/jsref/prop_win_status.asp.

Apparently, this functionality has to be turned on first in all major browsers, so apparently different browsers do different things when it's turned off. Chrome changes the value of the status property to a string, so it becomes the cryptic-but-familiar string "[object Object]", which has no entry_count property. Firefox leaves the object intact in the status property.

Bottom line: window.status is already being used for something else; use a different name for your variable.

EDIT:

As mentioned below, an even better way do do all this would be to encapsulate it in function scope, as long as you're not going to use it in other places anyway:

(function() {
   var myStatus = {...};
   // Do something with myStatus, preferably not document.write ;)
}());

var a = typeof myStatus; // a === 'undefined'.

This way, the variable will only be visible within the function scope.

这篇关于FireFox& JavaScript中不同的JavaScript对象文字行为谷歌浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆