保存id的元素的变量被存储在哪里? [英] Where does the variable holding an element with an id get stored?

查看:97
本文介绍了保存id的元素的变量被存储在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题( ID可访问元素)指出,如果一个元素有一个id,那么你可以访问它基于该id的变量名。它引起了我的兴趣,正如我在使用Visual Studio 2010开发过程中看到的这个变量一样。我做了一些好奇的测试,结果发现,$ code> document.getElementById()仍然是比使用变量名称快。所以,我开始试图透过窗口,认为它必须在窗口[idName] 中,在调试中,并使用 console.log (窗口),找不到实际存储变量的位置。

This question ( Element accessible with ID ) points out that if an element has an id then you can access it by variable name based on that id. It intrigued me, as I had seen this variable available when developing using Visual Studio 2010. I did some testing out of curiosity and it turns out that document.getElementById() was still faster than using the variable name. So, I began trying to look through the window, figuring it must be in window["idName"], in debug, and with console.log(window) and could not find where the variable was actually stored.

当html中定义了一个元素 < div id =foo> 它可以在javascript中使用变量 foo (我不是建议使用这个,不好的做法)。该变量存储在哪里?

When an element is defined in html with <div id="foo"> it is available in javascript with the variable foo (I am not suggesting to use this, it is bad practice). Where is that variable stored?

推荐答案

这是非标准的行为。其中(和)如果存储的是实现。

This is non-standard behavior. Where (and if) it is stored is up to the implementation.

使用 Firefox 15 在Linux上,我不得不深入2个原型对象来找到实际的对象。我在这个StackOverflow页面上运行了这个代码,并得到一个 true 结果。

Using Firefox 15 on Linux, I had to go 2 prototype objects deep to find the actual object. I ran this code on this StackOverflow page, and got a true result.

Object.getPrototypeOf(Object.getPrototypeOf(window)).hasOwnProperty("hlogo");






在Linux上的Chrome中, 。


In Chrome on Linux, it was one level deep.

Object.getPrototypeOf(window).hasOwnProperty("hlogo");






我实际上很惊讶的看到它在Firefox,但是由于Chrome跟随微软模式,所以我猜Firefox一定是觉得需要这样做。


I was actually surprised to see it in Firefox, but since Chrome followed the Microsoft pattern, I guess Firefox must have felt the need to follow suit.

不知道原型链有多深,可以运行一个循环,并将不同的对象添加到数组中,或者只是使用循环中的对象。

If you don't know how deep a prototype chain is, you can run a loop, and add the different objects to an Array, or just work with the objects in the loop.

var protos = [],
    obj = window;

while (Object.getPrototypeOf(obj) !== null) {
    obj = Object.getPrototypeOf(obj);
    protos.push(obj);
}

alert("The object had " + protos.length + " prototype objects");

这篇关于保存id的元素的变量被存储在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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