"JavaScript Advanced Programming Third Edition" Learning Notes (v) inheritance detailed __java

Source: Internet
Author: User
Object-oriented languages are inherited in two ways: interface Inheritance (only the method name), and implementing inheritance (inheriting the actual method). But in ECMAScript, the function name does not have much meaning, just the function body reference only, therefore, ECMAScript cannot implement the interface inheritance, only supports the implementation inheritance. Implementation of inheritance, mainly rely on the prototype chain to complete.
first, the prototype chain1. The basic idea of the prototype chain is to make a reference type inherit the properties and methods of another reference type using the prototype.
2. The relationship between constructors, prototypes, and instances
(1) Each constructor has a prototype object, and Func.prototype points to the prototype object.
(2) The prototype object contains a pointer to the constructor, Func.prototype.constructor equals Func, which can be understood as prototype and constructor are two opposite pointers, building bridges between constructors and prototypes.
(3) Each instance contains an internal pointer __proto__ to the prototype object, which is, of course, invisible to the developer.
(4) If the prototype of constructor A is set to an instance of constructor B, then the prototype of constructor A contains a pointer to another prototype, thus forming a prototype chain. This enables inheritance. When using an object's properties or methods, the parser searches the instance internally, cannot find the inside of the prototype of the search instance, and then finds no internal prototype of the constructor for the prototype of the search instance, looking up at the first level.
Small experiment
function Human () {

}
human.prototype.getsex=function () {return this.sex;}
Human.prototype.setsex=function (v) {this.sex=v;}

function Male () {
	this.setsex ("Male");
}
Male.prototype=new Human ();
Male.prototype.getage=function () {return this.age;}
Male.prototype.setage=function (v) {this.age=v;}


var tom=new Male ();
Tom.setage (a);

Alert (Tom.getsex ());//male
alert (Tom.getage ());//20
alert (Tom instanceof male);//true
alert (Tom instanceof Human);//true
alert (Tom instanceof Object);//true
alert (tom.constructor = Male);//false
Alert (Tom.constructor = = Human);//true
(5) The top of the prototype chain is object, which is why all reference types Instanceof object will return True. In other words, as long as the constructor behind Instanceof returns true in the prototype chain of the instance.
(6) Note that, as a result of inheritance, the prototype was rewritten, so Tom's constructor is no longer a male, but a male.prototype constructor human.
3. Use the prototype chain as an inheritance problem.
(1) As with the prototype pattern, you can use a prototype chain to inherit, and you will encounter multiple instances sharing the object (reference type) property. That's why reference type attributes are placed inside the constructor.
(2) A more serious problem is that when using a prototype chain to do inheritance, you must replace the prototype of the derived class with an instance of the base class, and the process of generating the instance of the base class cannot use parameters, which greatly reduces the flexibility of inheritance.
second, borrow the constructor function1. In order to solve the first problem of inheritance encountered by prototype chain, a method of borrowing constructors was presented. The idea is to perform the constructor of the base class as a normal function, but it must pass through the execution space of the derived class.
Small experiment
function Human () {
	this.sex= "please input";
	this.age=-1;	
}

The function Male () {
	human.call (this);//shows the base class constructor as a normal function call.
}
Male.prototype=new Human ();

var tom=new Male ();
var jim=new Male ();
tom.sex= "male";
alert (tom.sex);//male
alert (jim.sex);//please input
2. Of course, the construction does not pass the parameters of the use is still small, fortunately, apply and call are supporting the reference.
Small experiment
function Human (sex,age) {
	this.sex=sex;
	this.age=age;	
}

function Male (age) {
	human.apply (this,[' Male ', Age]);
}
Male.prototype=new Human ();

var tom=new Male ();
var jim=new Male ();
alert (tom.sex);//male
alert (jim.age);//25
alert (tom.age);//30
3. The borrow constructor solves the problem of referencing type properties, but there is no workaround.
Iii. Combinatorial Inheritance (pseudo-classical inheritance)1. This inheritance combines the prototype chain with the borrowed constructor. Using the use of the constructor to deal with attributes, using the prototype chain processing method. is actually equivalent to the "combination mode" mentioned when you create an object
Small experiment
function Human (sex) {
	this._sex=sex;
}
Human.prototype.getsex=function () {return this._sex;}
Human.prototype.setsex=function (v) {this._sex=v;}

function Male (name,age) {
	Human.call (this, "Male");
	This.name=name;
	this.age=age;
}
Male.prototype=new Human ();
Male.prototype.sayhello=function () {
	alert ("Hello, my name are" +this.name+ ", I ' M" +this.age+ "years old, I ' m a" + (thi S.getsex () = = "Male"? Boy ":" Girl ") +". ");	
}

var tom=new Male ("Tom");
var jan=new Male ("a few");
Jan.setsex ("female");
Tom.sayhello ();
Jan.sayhello ();
2. This is currently the most commonly used, but also to solve the most perfect inheritance, not one.

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.