Understanding of prototype and constructor attributes

Source: Internet
Author: User

1 Why Xx.constructor.prototype can access the prototype of the current object. ' str ' .constructor.prototype      ' str '. Constructor points to the constructor of the current object           (constructor). Prototype: The prototype property of the function                1 function has prototype attribute, so it can be accessed to     &N Bsp The prototype property of the          2 function will exist as the prototype of the instance when the instance is created.                     so ' str '. Constructor.prototype can access the prototype of the current object instance.  1         ' str ' .constructor  = = string     true.constructor = = boolean      (1). constructor = = number     {}.constructor = = object     []. constructor = = array     function () {}.constructor = = function  2    array.constructor    object.constructor    function.constructor    Number.constructor     string.constructor    Boolean.constructor resuLT: all: function function () {} inference:      constructors for built-in objects, all function function () {}, including function itself.       So JS all constructors for built-in objects are function function () {}  3      number.__proto__ = = = function.prototype //true     boolean.__proto__ = = = Function.prototype//true    &NBSP ; string.__proto__ = = = function.prototype //true     object.__proto__ = = = function.prototype  true     function.__proto__ = = = Function.prototype//true     array.__proto__ = = Funct ion.prototype   //true     regexp.__proto__ = = function.prototype //true     error.__proto__ = = = function.prototype   //true     date.__proto__ = = = function.prototype   //true 4     function.prototype.__proto__ = = Object.prototype      OBJECT.PROTOTYPE.__PROTO__ = = object.prototype    &NBSp number.prototype.__proto__ = = object.prototype     BOOLEAN.PROTOTYPE.__PROTO__ = = Object.prototype      STRING.PROTOTYPE.__PROTO__ = = object.prototype     ARRAY.PROTOTYPE.__PROTO__ = = object.prototype     regexp.prototype.__proto__ = = object.prototype      error.prototype.__proto__ = = object.prototype     DATE.PROTOTYPE.__PROTO__ = = Object.prototype  Corollary:     1 only functions have prototype properties      2 so XXX in Xxx.prototype above is a constructor      3 and 2 xxx.__proto__    So: XXX can be either a built-in object or a constructor. Judging by the situation.   Conclusion: All instances of Object are prototyped as Object.prototype  object.constructor = = Functionfunction.prototype.__proto__ = = Object.prototype; and: object.prototype.__proto__ = = null;  prototype 1 js objects have internal properties [[prototype]] Point to its prototype object. [[prototype]] is called the prototype attribute.  2 Internal properties cannot be accessed directly. Error:obj[[prototype], but can be accessed indirectly [[prototype]]    a  ECMA: Standard object prototype accessor object.geTprototype (object) (only Firefox and Chrome have implemented this accessor so far);    b  non-standard accessors: __proto__ (except IE)     c  Obj.constructor.prototype js Everything is an object, so the function is also an object. Because the object has internal properties [[prototype]], the function also has intrinsic properties [[prototype]].  Second: The function owns both the property Prototype.prototype property and [[prototype]] are not the same property. The prototype property points to an object, called a prototype object. So: The prototype properties of a function (functions ' s prototype property) and the actual prototype of the function (prototype) have no relation to the role of the prototype object: When the function fn is used as a constructor, the internal properties of all instances that it creates [[Prototyp E]] prototype property that points to FN. The role of prototypes: 1 building a prototype chain when an object calls a method, if it does not exist in its own way, it will look in its prototype until the top of the prototype chain. The role of the prototype chain: the methods required by multiple instances can be extracted and put into the prototype, which only needs to be defined once, and all objects that implement multiple object sharing methods Constructorjs have the constructor property, pointing to its constructor. The prototype object of the function is also an object, so the prototype object also has the constructor property.    And JS set the rules: Fn.prototype.constructor = = fn; That is: the constructor field of the prototype property of the function, which points to the owner of the current prototype property, which is the constructor itself. by Fn.prototype.constructor = = fn; The derived conclusion: the constructor property of the instance of the constructor points to its constructor.          Derivation: var a = new FN (); First Look in a itself: no constructor attribute found. Then look up the prototype chain, find the prototype of a, that is, Fn.prototype, and find the constructor attribute so it is equivalent to the constructor of the instance of the constructor. The sex always points to its constructor.

Understanding of prototype and constructor attributes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.