Detailed explanation of the relationship between the constructor and the protocol chain in JavaScript

There is no Class concept in JavaScript, and its Class concept is achieved by constructor ( Constructor ) with the prototype chain ( prototype ).

1. Constructor: The initialization object when creating an object is always the same as the new key.

The configuration function has the following features:

1, the THIS within the constructor points to the current instance object.
2, instantiate the current object using the New keyword.

3, the first letters of the constructor, distinguish between ordinary functions.
    4, instance objects can inherit the properties and methods in the constructor. However, between the same object instance cannot share properties.
  • 2. Prototype: is an object that implements the object’s attribute inheritance. The object in JavaScript points to prototype objects through Proto, can access
  • 3. of
  • 3. Constructor and prototype:

Function demo () {} var Demo = new demo () var data = demo.prototype = function () {} console.log (Data.constructor) console.log (data.prototype) Console.log (data.prototype) .__ proto__) console.log (demo.constructor.prototype) Console.log (Demo.constructor) Output: {constructor: ƒ} constructor: ƒ Demo () __ proto__: Object ƒ Function () {[native code]} {constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …} {Constructor: ƒ} ƒ Demo () {}
 From the above output results, it can be seen:    Constructor _ _Proto__ point to prototype objects;  CONSTRUCTOR of 
Points to constructor function; prototype PROTOTYPE .__PROTO__ is equal to the CONSTRUctor.Prototype of the Object .__ProtoToTYPE pointing to prototype;
instance point to constructor

! [1460000018155881] [1]

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment