The deep understanding and application of JavaScript prototype chain __java

Source: Internet
Author: User
Tags function prototype

* Today, a summary of JavaScript prototype prototype and *____PROTO____ prototype chain, to understand that we have a deep understanding of JS, encapsulation of common tips is helpful.

ES5 JS itself is not class, in the ES5 JS class is function functions, and function itself is the object.

One, JS in the inheritance is through the prototype chain __proto__ to achieve, objects and objects and prototype prototype (also object) is through the __proto__ prototype chain to link, the specific process we come to the code description.

function  obj (name) {
        this.name=name,
        this.age= "This.fun=function"
        () {return
            this.name
        };
        "obj.prototype.city=";
Obj.prototype.fun1=function () {return
    this.age;
};
var obj1= new obj ("Laowang");
var obj2= nwe obj ("Hello");

This section of code format should be very common, but there is a problem, var obj1 =new obj ("Laowang") is in the global scope, if the page this writing more, it is easy not to pollute the overall situation.
Can be written like this:

(function () {
  var obj1 = new obj ("Laowang");
  This can also work properly and not be contaminated with the Global Environment
}) ()

Back to the point, when we execute the following code:

Obj1.name;    Results  Laowang
obj1.city;    Results of the
Obj1.fun ();   Results  Laowang 
obj1.fun2 ();  Result  23

We will see that OBJ1 has the attributes and methods of obj, as well as the method of the OBJ prototype.

Analysis: obj currently has the name and age private properties and fun private methods, we added the city attribute to the OBJ prototype, and the Fun1 method

"Obj.prototype.city="; 
Obj.prototype.fun1=function () {return
    this.age;
};

And the constructor we created with new, and assigned to the object obj1, this process JS did a similar thing for us: (Here the code just makes sense)

     "Hello")  //executing the OBJ function, pointing the O object pointer to the OBJ function, and O having the properties and methods (references) of the OBJ prototype (reference) return
     o;
}) ()

//We can try it,
Obj1.__proto__===obj.prototype 

Indicates that Obj1 's prototype chain points to Obj's prototype
__proto__ method not to get the standard method of instance prototype, standard method please use object.getprototypeof () to get the instance prototype

Obj1.name===obj2.name//Result true indicates that obj1 and obj2 collectively reference obj's property name

To be clear, a function constructed with new is not a prototype, for example:

Obj1.prototype  //Results Underfind confirmed our conjecture.

Second, the above is finished, but there is a problem, if I want to create a function myobj, let the function have the OBJ prototype methods and properties what to do.

1. You may do this first:

var myobj = function () {};
Myobj.prototype=obj.prototype;
Myobj.city//Results Underfind

This is because the __proto__ in MyObj's prototype points to the prototype of obj. MyObj does not implement the prototype methods and attributes that inherit obj, and it also poses the problem that when you modify the properties (city) or method (FUN1) in the MyObj prototype, the properties (city) and Method (OBJ2) in Obj1 and FUN1 will also change.

2. Or have you changed this:

Myobj.__proto__=obj.prototye

But the problem is __proto__ is not standard, in IE and Firefox sometimes error.

3. The correct wording should be written as follows:

Object.creat=function (aa) {
  var o = function () {};//Create a new function
  O.PROTOTYPE=AA//Let the prototype of the function equal the prototype of obj return
  new O () ; Returns the constructor of an O
myobj.prototype=object.creat (obj.prototye);
Let the function prototype wait with the Objet.creat object,

At this point, the prototype object of MyObj has the prototype method and attribute of obj, and when MyObj's prototype properties and methods are changed, the properties and methods of the OBJ prototype will not be shadow

But there are a few more questions at this point:
1. MyObj.prototype.constructor returns the instance obj, itself constructor, which points to its own instance in the prototype, and at this point, Both MyObj.prototype.constructor and Obj.prototype.constructor point to instance obj;
2. The myobj.city result would be underfind; We can only access the city property by accessing the MyObj prototype;
3. If the second problem is solved, we will find Myobj.fun1 (); The result is underfind;

All right, let's get this one.
First question: We can do this.

Myobj.prototype.constructor=myobj; Let MyObj's prototype constructor point to myobj itself.

Second question: We can do this:

var ob= new MyObj (); A new instance is created using new.

A third question: we can do this:

var myobj = function () {
        obj.call (this);  Resolve Ob.fun1 () Underfind
    };

Okay, the problem is basically solved, and finally I put all the code on it.

     function  obj (name) {
        this.name=name,
        this.age= "This.fun=function"
        () {return
            this.name
        };
        "obj.prototype.city=";
     Obj.prototype.fun1=function () {return
        this.age;
     };
     var obj1= new obj ("Laowang");
     var obj2= new obj ("Hello");

Operation within the new constructor (code only)
    //obj1= (function () {
    //  var o={};  O.__proto__=obj.prototype;     Obj.call (o, "hello")
    //return     O;
    // })();
    var myobj = function () {
        obj.call (this);  Resolve Ob.fun1 () Underfind
    };

    Object.create=function (aa) {
        var o = function () {};
        O.PROTOTYPE=AA;
        return new O ();
    }
    Myobj.prototype=object.create (obj.prototype);

    Correcting the pointing myobj.prototype.constructor=myobj of the constructor of the MyObj prototype
     ;

     Resolve to access
     var ob= new MyObj () directly with ob.city;

OK, here is the end, if there is anything wrong, welcome to leave a message.

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.