The prototype in JS, some ideas of inheritance

Source: Internet
Author: User

Recently saw a other people write JS class library, suddenly JS in the prototype and inherited a number of ideas, before also seen some of the content, but it is not very clear, these days using free time, to this piece of understanding, feeling or there is no way, the idea is not so organized, only as a share,

First, instanceof

In JavaScript there is the instanceof operator, which is a two-tuple operator, using the method Instancea instanceof A, the return value is a Boolean (Boolean), meaning to determine whether Instancea is an instance of a, Its essence is to judge a.prototype===instancea.__proto__, such as

function F2 () {    var f=function() {}    var test=New  f ();     instanceof f); // true    Console.log ((f.prototype===test.__proto__)); // true}

The top two prints are true. The description test is an instance of F, and the __proto__ property of test points to the prototype object of F, that is, the prototype property of F is an object, and this object is an instance of F.

Second, JS in the object

In JS, everything is an object, the object is divided into function objects and ordinary objects, commonly used functions are actually function objects, such as

    // Function Object    var f=function() {}    var f2=new function (' str ', ' Console.log (str) ') )    function  f3 () {}    // Ordinary object    var o= New Object ();     var o2={}    var o3=New F ()

The above see F, F2, F3 is a function object, O, O2, O3 is a normal object.

The difference between a function object and a normal object:

All objects created using the new function () are function objects, and F and F3 two ways, and are ultimately created using the new function () method;

When defining an object, the object contains some predefined properties, such as prototype, __PROTO__, where the prototype property is only available for function objects, and __proto__ is available for all objects, so the object can be __proto__ property to determine whether an object is a function object or a normal object, such as

    // function Object    var f=function() {}    // Ordinary object    var o=New Object ();    Console.log (f.prototype); // Object {}    Console.log (O.prototype); // undefined


From the top you can conclude that the prototype property exists for a function object, while the prototype object of a normal object is undefined.

Three, the prototype chain

It is known from the top that all objects have a __proto__ property, which points to the prototype object of the function object that created it prototype, and we use this chain of __proto__ properties as the prototype chain, as below is a prototype chain,

, take person as an example, explain the prototype chain,

var person=function() {}var person1=New person ();

1, person is a function object, Person1 is an instance of person

2. The __proto__ attribute of Person1 is the prototype object of person Person.prototype

3. Because the prototype object of person Person.prototype is an object, it also has a __proto__ property, which points to the prototype object of Object Object.prototype

4. Object Object.prototype is an object, it also has a __proto__ property, the prototype object of this property is null.

Iv. some of the inheritance

In JS we define a function object, such as

var person=function() {}

Above we define a function object, which does not have any properties, is an empty object, because it is an object, so you can add properties for it,

var person=function() {}person.name1= ' js 'console.log (person.name1)//  JS

The above code adds a name attribute to person and is assigned JS, and the value of the Name property can be printed out
But when we create an instance of person Person1, the following

var function (){};    Person.name1= 122;    Console.log (person.name1);     var person1=New person ();    Console.log (person1.name1); // undefined

You can see that Person1 does not have a name1 attribute, so how can I ensure that the instance of person has a name1 attribute?

var function (){};    Person.name1= 122;    Use the prototype object to add properties to an object so that an instance will have this property
person.prototype.name1= ' n '; Console.log (person.name1); var person1=New person (); Console.log (person1.name1); // A

Above, using person.protoype.name1= ' 12 ', so that all instances have the name1 attribute, the attribute added in this way, the attribute is used as the common property of the instance when the instance is generated.

Write so much first, there are inappropriate, welcome to point out. Thank you

The prototype in JS, some ideas of inheritance

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.