简体   繁体   中英

What does it mean by “actions are taken” in ECMAScript 5?

Here is the excerpt.

15.2.2.1 new Object ( [ value ] ) # Ⓣ
When the Object constructor is called with no arguments or with one argument value, the following steps are taken:

If value is supplied, then If Type(value) is Object, then

If the value is a native ECMAScript object, do not create a new object but simply return value.

If the value is a host object, then actions are taken and a result is returned in an implementation-dependent manner that may depend on the host object.

I'm just wondering what's the different between passing in a Native Object and passing in a Host Object to the Object constructor. In other words, if I call new Object(document), what happens behind the scene? I cannot see any difference by checking the return value.

Thanks in advance.

For the native objects : javascript has not to create the new object and simply returns the values.

But for the host objects : javascript first create a new object and then only returns the values.

That's what the spec is saying. Hope you understand!

What the excerpt is saying is that if value is a host object such as a Document , then it's up to the implementation to decide what new Object(value) will do ("actions are taken […] in an implementation-dependent manner that may depend on" value ) and what value it will return ("a result is returned in an implementation-dependent manner that may depend on" value ).

Even if value is a host object, most of the time new Object(value) will do nothing and return value (just as if it were a native object). And in the specific case of a Document , I would strongly expect that behavior. But the ECMAScript standard does not require this. (And the DOM specification, which defines the Document type, doesn't seem to require it, either.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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