JS prototype and prototype chain understanding constructor constructors

Source: Internet
Author: User
Tags function prototype

I. Object: Normal Object Function object

Two. Constructor features: 1. A new instantiation is required, and the This object is used internally to point to the instance object 2 that is about to be generated. Uppercase letters, used to distinguish common functions

function Person (name) {    the name=name}var person1=New person (' Xiaohong ') )var person2=New person (' Lili ')

Person1.constructor=person constructor points to constructors, person's built-in properties Person.prototype (function objects), each prototype object has a constructor property, Pointer to the function where the prototype property is located

namely Person1.constructor=person2.constructor=person.prototype.constructor

Inheriting through prototypes

Three. __proto__, each object has this property pointing to the prototype that created his constructor, which is Person1.__proto__=person.prototype

// var obj={} var obj=New  Object (); Obj.constructor= = =objectobj.__proto__= = = Object.prototype

Person1.__proto__ is Person.prototype.

Person.__proto__ is a person is a constructor, function, or function.prototype

Person.prototype._proto__ is the Person.prototype is the prototype object, the objects, that is Object.prototype

Object.__proto__ is Objext is the structure of the number of rows function that is Function.prototype

OBJECT.PROTOTYPE.__PTOTO__ is the prototype of the Object.prototype's prototype object __proto__ to the constructor, which is null at the top level

Four. Math and JSON are math.__proto__===object.prototype that exist as objects

Five. Function.prototype is the only typeof Function.prototype is a Function prototype, and the prototype of other constructors are object

Six. Note the point of constructor for example

functionAnimal () {}animal.prototype.age= ' 20 'functionCat (name,color) { This. Name =name This. color =Color This. name2 = ' 11112222 '}cat.prototype=NewAnimal (); Cat.prototype.type= "Cat"Cat.prototype.eat=function() {Console.log (' Love to eat fish ')}functionDog (name) { This. name=name} Dog.prototype=NewAnimal ();D Og.prototype={getName:function() {Console.log (' Gougou ')    }}

Cat.prototype.constructor = = = Animal Dog.prototype.constructor===object is not the same, the latter is rewrite Animal.prototype, the former is modified.

Seven.

    • Prototype and prototype chain is a model of JS implementation inheritance.
    • The formation of the prototype chain is really, __proto__ rather thanprototype

JS prototype and prototype chain understanding constructor constructors

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.