Built In JavaScript Properties - egnomerator/misc GitHub Wiki
"Prototypes Explained, Part 1"
- This link is to a video within a Pluralsight course "Advanced JavaScript" by Kyle Simpson
- This is a link to another page within this github wiki which uses the
getObjectInfo()
function discussed in this wiki page
- This is a link to another page within this github wiki
This wiki page is only intended to cover a small number of built-in properties as ancillary information to other wiki pages here.
"Internal Prototype Linkage" Prototype
What is this phrase "internal prototype linkage" used throughout this page?
All ordinary objects have an internal slot called Prototype. The value of this internal slot is either null or an object and is used for implementing inheritance
- This is a link between one object and the prototype object it's linked to
- This link cannot be accessed directly which is why it is called internal
- There are built-in publicly exposed accessors for these internal linkages
- When an internal prototype linkage is returned, this linkage automatically returns the linked prototype object (you are never actually able to get the internal property Prototype)
- This can produce a null value, and this happens only when getting the internal prototype linkage of the final object in the prototype chain (which is Object.prototype:
Object.getPrototypeOf(Object.prototype)
returns null)
(it's helpful to reference the diagram and related explanations here):
-
.prototype
: a built-in property of functions which points to the calling function's corresponding prototype object- Every function has its own corresponding prototype object
- Every function can be used as a constructor to create a brand new object linked to that function's prototype object
- Calling
.prototype
on a non-function object will result inundefined
-
Object.getPrototypeOf([object])
: returns the passed-in object's internal prototype linkage -
[object].__proto__
: alternative toObject.getPrototypeOf([object])
-
.__proto__
is a getter/setter function of the root JavaScript prototype object, Object.prototype; when.__proto__
is called on an object, this causes a traversal up the prototype chain to Object.prototype (because.__proto__
is a property that exists only on Object.prototype) whereObject.prototype.__proto__
is called which returns the internal prototype linkage of the object that originally made a call to.__proto__
- E.g.
o.__proto__
causes the following behavior:-
.__proto__
is discovered not to exist ono
- Traversal up prototype chain to
Object.prototype
-
.__proto__
exists onObject.prototype
soObject.prototype.__proto__
is called - Return internal linkage of
o
-
- The use of
.__proto__
is generally discouraged. Although it is supported in recent versions of all browsers and was even standardized in ES6, it was added in ES6 as a deprecated feature to support legacy code
-
-
.constructor
: prototype objects have a.constructor
property which points to their associated function object. Calling.constructor
on a non-prototype object causes traversal of the prototype chain to the calling-object's prototype object on which.constructor
is called returning the associated function object -
Function.prototype
:- This is a strange outlier in the language (see ES 2016 section 19.2.3)
-
A couple key quotes from this documentation:
NOTE The Function prototype object is specified to be a function object to ensure compatibility with ECMAScript code that was created prior to the ECMAScript 2015 specification.
The Function prototype object does not have a prototype property.
-
- This is technically an exception to the "Every function ..." blanket statement bullet points under
.prototype
above, but practically and functionally speaking, thisFunction.prototype
object is the prototype object ofFunction
(even though it is technically a function object)
- This is a strange outlier in the language (see ES 2016 section 19.2.3)