Prototype JavaScript and Inheritance

Continue to think on the basis of a created object on the top. Objects created out, but every time to re-create the object too much trouble. Students are people too, no need to re-write head student, can be extended by the people. This leads to the inheritance.

Prototype chain

js inherited by the method used to achieve it? Prototype object is to replace instances of another class. Because of the prototype chain, the new object will have a lot of properties and methods without having to start over we create.
example:

function Person(name) {
    this.name = name;
    if (typeof this.sayName !== "function") {
        Person.prototype.sayName = function() {
            return this.name;
        };
    };
}

function Student(score) {
    this.score = score;
}

Student.prototype = new Person("foo");
Student.prototype.constructor = Student;

var student = new Student(100);

alert(student.name);    //"foo"

alert(student.sayName());   //"foo"

Student constructor name attribute is not defined, all objects created by the new new Student () does not name attribute. And Student prototypes where there is no such property. But when we put the prototype into a Student's Person instance, when this example is the name attribute, which is the prototype of Student's with this property, so the student can access the name attribute. This is the inheritance of meaning, we do not need to define the name attribute Student, just let him inherit an existing class on the line.
That is the way we sayName added to the Person of the prototype, Person instance (ie Student prototype) is not on this approach, and why Student examples of this approach can access it. This is the magic of the prototype chain. I can not find in your body, go find your prototype.
Such objects produced by us have a succession: 1. their properties and methods generated constructor 2. properties and methods defined in the parent class instance attributes and methods of the parent class 3. prototype.
If the combination of creating a parent class and instance constructors mode or dynamic prototype prototype pattern, then, the only instance of the parent class attributes, methods on the parent class prototype.

Now let's think about a problem. Such objects produced in line with our requirements right. First thing clear. We generally produce objects that each instance has its own properties (rather than share), while the method is hoping to share.
It is produced by inheriting this object? You can see, we have the property due to the subclass prototype definitions, so all subclasses will share examples of properties on the prototype, this is not what we want.
To that each sub-class instance has its own properties how to do it. We know constructor can do this (instance generated by the constructor has its own properties). So we had a tip: borrow constructor.

Borrowing a combination of inheritance and constructors

Borrowing ideas constructor is to achieve sub-class instance each with its own attributes defined by the original property on the child class prototype in the constructor of the subclass. Defined directly on the line in the sub-class constructor. But we look you will find examples of prototype sub-class is generated by the parent class constructor ah, that is a subclass of property on the prototype of all the parent class constructor is defined.
Then we only need to subclass "borrowed" the parent class constructor to define a parent class instance of property is not the same as hair eliminating the need for a definition of a trouble himself yet.
Look at an example:

function Person(name) {
    this.name = name;
    if (typeof this.sayName !== "function") {
        Person.prototype.sayName = function() {
            return this.name;
        };
    };
}

function Student(name, score) {
    Person.call(this, name);
    this.score = score;
}

Student.prototype = new Person("foo");
Student.prototype.constructor = Student;
var student = new Student("bar", 100);

alert(student.name);        //"bar"

Student borrowing by the constructor each instance has its own name attribute. Naturally covers the prototype name attribute (Person instance), while still further Person prototype. perfect.
This inheritance is a combination of inheritance. Inheritance is a combination of attributes defined to the sub-class constructor (parent class constructor can borrow), the method to define the prototype chain, the chain prototype technologies and techniques borrowed constructors so called combined composition inheritance.
This is our future inheritance patterns often use.
One point to note is that when we look at inherited eye can not be too narrow, just look at this example might think it is necessary to inherit it be too much trouble, directly generate dynamic prototype model subclass object is not finished. But when we have tens of thousands of sub-categories to define you know why inherited.

Prototypal inheritance

function object(o) {
    function F() {}
    F.prototype = o;
    return new F();
}

As long as we have a parameter passed to this function, the function can return an object instance, this is what we object instance to prototype parameters, that returns an instance of the inherited parameters to pass to them.

Such objects returned it really good? Property is independent, whether the method to share? Well, you can do it.
We can borrow in F () constructor o constructor (that is, o) and then in the Add method on the prototype or the prototype F o, but if we really did inherit a combination of that and what difference does it make? If that is the only benefit package up, after inheriting it convenient, direct call this function just fine.
But in fact JavaScript help us a good package, but not by that package we want is for the most primitive function (function code above) the package, so called prototypal inheritance Well, no combination. We called directly Object.create (o) is equivalent to calling the above method. But the property thus produced is shared (prototypal inheritance thing), so do not understand what's the use of this package. Prototypal inheritance rarely used alone, most of the time using a combination of inheritance.

Parasitic inheritance

But also a succession no meaning

function createStudent(person) {
    var student = Object.create(person);
    student.sayScore = function() {
        alert(100);
    };

    return student;
}

Enhanced little inherited, than the prototype, but the objects produced in this way each instance has its own method. It is not very useful.

Combined parasitic inheritance

Now let's think a combination of inheritance. We subclass Each instance has its own properties, completely covering the sub-class prototype (parent instance) in the property. Then we want to do it this instance of the parent class, but we want to find the parent class prototype by this instance of the parent class, and then call the method prototype. So we really need to create an instance of the parent class as a subclass of the prototype it? We can complete the prototype copy of the parent class as a subclass of the prototype, so we do not climb down the prototype chain does not need to create an instance of the parent class is not used as a sub-class prototype.
The resulting parasitic combined inheritance.
For example, we want to inherit Person Student

function Person(name) {
    this.name = name;

    if (typeof this.sayName !== "function") {
        Person.prototype.sayName = function() {
            return this.name;
        };
    }
}

function Student(name, score) {
    Person.call(this, name);
    this.score = score;
}

Student.prototype = Object(Person.prototype);
Student.prototype.constructor = Student;

var student = new Student("foo", 100);

This is the real perfect! You are my prototype prototype.

发布了25 篇原创文章 · 获赞 46 · 访问量 2万+

Guess you like

Origin blog.csdn.net/huster1446/article/details/62418618