简体   繁体   English

JavaScript如何区分对象?

[英]How does JavaScript distinguish between objects?

I have wriiten 我扭了

document.createElement("p");
document.createElement("p")

How does Javascript intepreter knows do distinct between those two? Javascript解释器如何知道两者之间的区别?

Id ? ID ( what is the Js property )? (Js 属性是什么)? maybe something else ? 也许还有别的吗?

In this particular case: 在这种情况下:

document.createElement("p");
document.createElement("p")

the JavaScript runtime doesn't worry about the elements at all , because you didn't save the values returned. JavaScript的运行时不担心在所有的元素,因为你没有保存返回的值。 They're thrown away. 他们被扔掉了。

If you had written, 如果你写过

var p1 = document.createElementById('p');
var p2 = document.createElementById('p');

well then you've got two separate variables, and so you're keeping track of the difference. 那么,您就有了两个单独的变量,因此您要跟踪差异。

It's important to be aware that the JavaScript interpreter itself doesn't really care too much about your DOM nodes. 重要的是要注意,JavaScript解释器本身并不太在乎您的DOM节点。 That's not really it's business. 那不是真的吗? If you call methods and create objects, it's your problem to keep track of them. 如果调用方法并创建对象,则跟踪它们是您的问题。

Let's pick another real-life example: How does a human distinguish one orange from another one, in a basket? 让我们再举一个现实的例子:一个人如何在篮子里区分一个橘子和另一个橘子? I look inside the basket, and notice that there're multiple orange-coloured, ball-shaped items 我往篮子里看,发现里面有多个橙色的球形物品

The JavaScript interpreter internally keeps track of the created objects. JavaScript解释器在内部跟踪创建的对象。 When an object isn't referred by anything (variables), the built-in Garbage Collector destroys the object. 当某个对象未被任何内容(变量)引用时,内置的垃圾收集器将销毁该对象。

It doesn't. 没有。 createElement returns the object reference but if you don't assign it to anything or directly use it, it's lost. createElement返回对象引用,但是如果您不将其分配给任何对象或直接使用它,则它将丢失。

var firstp = document.createElement("p");
var secondp = document.createElement("p");

Then you can assign an id or whatnot: 然后,您可以分配一个ID或其他:

firstp.id = "first";

Keep in mind though, that this is not in the DOM yet. 但是请记住,这还不是DOM中的。 You have to insert it somewhere before it can be seen by the user/found with getElementById. 您必须将其插入某个位置,然后用户才能使用getElementById找到它。

For that you can do something like this: 为此,您可以执行以下操作:

document.body.appendChild(firstp);

It doesn't. 没有。 When you call document.createElement() , you create an element and it's inserted into the DOM. 调用document.createElement() ,您将创建一个元素并将其插入DOM中。 JavaScript can then grab certain p elements by ID, for example, or grab other p (and other) elements with getElementsByClassName() . 然后,JavaScript可以按ID抓取某些p元素,或者使用getElementsByClassName()抓取其他p (和其他)元素。

If you'd assigned the returned values to variables, you'd be able to keep track of which one was which by referencing different variables. 如果将返回值分配给变量,则可以通过引用不同的变量来跟踪哪个是哪个。 This is you distinguishing between them, not JavaScript, though. 不过,这是区分它们,而不是JavaScript。

You can refer to them by their index in the array of elemnts: 您可以通过它们在元素数组中的索引来引用它们:

document.getElementsByTagName('p')[0]

You can assign id to them 您可以为其分配ID

document.getElementsByTagName('div')[0].id = 'first'
document.getElementsByTagName('div')[1].id = 'second'

And refer to them by id 并通过id引用它们

document.getElementById('first')

It is up to you how you do it really... 具体取决于您的实际操作方式...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM