Why do object literals in javascript preserve unnecessary DOM references?

From this document,

Do not do this

car = new Object(); car.make = "Honda"; car.model = "Civic"; car.transmission = "manual"; car.miles = 1000000; car.condition = "needs work"; 

Do it instead

 car = { make: "Honda", model: "Civic", transmission: "manual", miles: 1000000, condition: "needs work" } 

Because the

This saves space and unnecessary DOM links.

But the DOM just manipulates the object in HTML, XHTML or XML. The above has nothing to do with the DOM.

It is not right? Or am I missing something? Can someone help me understand what the DOM is mentioned in this article?

+6
source share
2 answers

There are actually two points here:

1) It reduces the number of executions of statements from 6 to 1. I'm not sure if this is faster in practical terms, but theoretically it should be. At least it makes the code cleaner and more understandable.

2) If this code is executed in a browser, the car object is added to the DOM because it is added to the window object.

This code will warn "LOL":

 var foo = "LOL"; alert(window.foo); 
+1
source

I think the author wanted to write links to objects. DOM links are meaningless.

+4
source

Source: https://habr.com/ru/post/894400/


All Articles