有没有办法在Internet Explorer中使用window.hasOwnProperty()? [英] Is there a way to use window.hasOwnProperty() in Internet Explorer?

查看:103
本文介绍了有没有办法在Internet Explorer中使用window.hasOwnProperty()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法,在Firefox中完全有效,我可以用它来确定特定javascript对象的实例的名称(请不要问我为什么需要它...)。
Fr示例:

I have a method, that perfectly works in Firefox, with which I can determine the name of an instance of a particular javascript object (please don't ask why I need it...). Fr example:

var temp = new String("hello!");
var theName = getVarName(temp); //returns "temp"

此方法使用window.hasOwnProperty(),它不起作用在Internet Explorer中:有什么建议吗?

This method uses "window.hasOwnProperty()" which doen't work in Internet Explorer: any suggestions?

推荐答案


我有一个方法,在Firefox中完美有效,我可以用它来确定特定javascript对象的实例名称

I have a method, that perfectly works in Firefox, with which I can determine the name of an instance of a particular javascript object

我认为你不这样做,因为那是不可能的在JavaScript中。 JS是一种按值调用的语言;当你写:

I don't think you do, because that's not possible in JavaScript. JS is a call-by-value language; when you write:

var temp= 'hello';
getVarName(temp);

与说法完全相同:

getVarName('hello');

此时temp作为变量的引用将丢失。我猜你的getVarName函数基本上是这样的:

At which point the reference to ‘temp’ as a variable is lost. What I'm guessing your getVarName function does is basically this:

function getVarName(value) {
    for (var name in window) {
        if (window[name]===value)
            return name;
    }
}

这适用于IE和其他没有Object的浏览器。 hasOwnProperty();它只会返回与参数匹配的任何全局变量的名称。可以将hasOwnProperty()调用添加到此函数中,通过仅允许窗口的直接属性(其充当全局变量,包括您明确设置的那些)而不是其任何原型来稍微改进它。我猜这是你的函数版本正在做什么,但实际上它的效果很小,因为几乎没有任何东西继承到原型的'window'。

This will work on IE and other browsers without Object.hasOwnProperty(); it will simply return the name of any global variable that matches the argument. The hasOwnProperty() call can be added to this function to refine it a little by only allowing direct properties of window (which act as global variables, including the ones you set explicitly), and not of any of its prototypes. I'm guessing that's what your version of the function is doing, but in practice it has very little effect since almost nothing inherits into ‘window’ by prototype.

你'通过在显式的String对象中装入你的'hello'来稍微混淆一些东西(这是非常不寻常的,很少是一个好主意),这使得有可能使用===身份获得两个不同的'hello'对象,这些对象是不可分辨的比较器,所以这将工作:

You're confusing things a little bit by boxing your 'hello' in an explicit String object (which is very unusual and rarely a good idea), which makes it possible to have two different 'hello' objects that are disinguishable using the === identity comparator, so this will work:

var a= new String('hello!');
var b= new String('hello!');
getVarName(a); // 'a'
getVarName(b); // 'b' - distinguishable object from a

但这仍然不能阻止你做:

But that still doesn't stop you from doing:

var a= new String('hello!');
var b= a;
getVarName(a); // 'a' or 'b', depending on implementation
getVarName(b); // the same 'a' or 'b' as in the previous call

所以,尽管你可以如上所述,相当无害地丢失了hasOwnProperty()调用,你正在做的事情无法正常工作,你应该看看一个更好的方法来实现你正在做的事情。

So, whilst you can fairly harmlessly lose the hasOwnProperty() call as above, what you're doing can't really work properly and you should probably look at a better way of achieving whatever it is you're up to.

这篇关于有没有办法在Internet Explorer中使用window.hasOwnProperty()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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