What is document.writeln (Object.prototype); in javascript print?

I tried and got the following output:

[object of object]

I am familiar with adding new functions to Object.prototype so that every object in the program inherits this new function. But I'm a little curious to know how this was implemented. I assume Object should be of type

{ name: expression, name: expression, ... } 

I also assume that Object.prototype should be the key to one of the Object properties, and the value of this property is "Object Object". Now, please let me know if I understood correctly?

I am also wondering what the difference is between “object” and “object” in “object of object”. I am also wondering if any of the above objects are related to the previous ".prototype". Please clarify?

When I tried to print Object.prototype.object, Object.prototype.Object, Object.prototype [object], Object.prototype [Object] and Object.prototype [0], I always get undefined. If the prototype has no property, how did I get [object Object]?

+4
source share
1 answer

object always present, and object proceeds from the value of the internal property [[Class]] , therefore

 Object.prototype.toString.call([]) === "[object Array]"; Object.prototype.toString.call("") === "[object String]"; Object.prototype.toString.call(new Date) === "[object Date]"; 

This is described in 15.2.4.2 :

15.2.4.2 Object.prototype.toString ( ) # Ⓣ Ⓔ Ⓡ

When the toString method is called, the following steps are performed:

  • If this value is undefined, return "[object Undefined]" .
  • If this value is null, return "[object Null]" .
  • Let O be the result of calling ToObject, passing this value as an argument.
  • Let class be the value of the [[Class]] internal property of O.
  • Returns the String value that is the result of combining the three strings "[object " , class, and "]" .

The intrinsic properties are a bit confusing. You can read them on 8.6.2 :

This specification uses various internal properties to define the semantics of object values. These internal properties are not part of the ECMAScript language. They are defined by this specification for explanatory purposes only. An ECMAScript implementation should behave as if it were created and controlled by internal properties in the manner described here. Internal property names are enclosed in double square brackets [[ ]] .

By the way, the trick Object.prototype.toString.call used by jQuery and other libraries in its isArray functions, since the internal property is the same for all frames, although each frame has its own version of the built-in Array .

+3
source

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


All Articles