When it comes to inheritance, it's mostly about the object-oriented reference JavaScript advanced programming JavaScript program full solution
Example 1
function Person (name,age) {
THIS.name = name;
This.age = age;
This.show = function () {
Console.log (This.name,this.age);
}
}
var person1 = new person (' haha ', 20);
var person2 = new Person (' Kaka ', 22);
Console.log (person1.show = = person2.show);//false
The following steps occur when you first create an object in the form of a constructor
(1) Create an object
(2) Assign the function of the constructor to this object (this object is pointed to)
(3) Executing the code in the constructor (adding properties to the new object)
(4) Return this object
But the problem is that the method of the generated object is recreated in the instance, that we do the same task to create two methods (objects) So consider the prototype chain
Each function created has a prototype attribute, which is a pointer to an object (prototype object) that contains the properties and methods shared by all instances of a particular type, which is the prototype pattern
Rewrite the example above
function Person (name,age) {
THIS.name = name;
This.age = age;
}
Person.prototype.sayName = function () {Console.log (this.name)};
var person1 = new person (' haha ', 20);
var person2 = new Person (' Kaka ', 21);
Person1.sayname ();
Person2.sayname ();
Console.log (Person1.sayname = = = Person2.sayname);//true
The constructor of the person points to the prototype object Person.prototype and the constructor property of the prototype object refers to the constructor of the person, which means that when we instantiate an instance of an object, The prototype object invokes its constructor property to instantiate the object, and the instantiated object has a property that points to its prototype
In this case, you see that there are two parts of the property or method in the person constructor of the property or method Person.prototype the prototype object's properties or methods are searched in the following order
(1) The property of the object itself (the person constructor in the example above)
(2) Properties of objects referenced by implicit links (constructor prototype objects)
(3) property of an object referenced by an implicit link to an object in item 2nd
(4) repeatedly by the rules of the 3rd find until all is found (the end of the lookup is the Object.prototype object)
The method of finding properties or functions is based on a chain structure, which is what we call the prototype chain, the prototype chain in JavaScript as the main way to implement inheritance, the basic idea is to use the prototype to let one reference type inherit another reference type property or method
function Supertype () {
This.property = true;
}
SuperType.prototype.getSuperValue = function () {
return this.property;
};
Function subtype () {
This.subproperty = false;
}
Subtype.prototype = new Supertype ();
SubType.prototype.getSubValue = function () {
return this.subproperty;
};
var instance = new Subtype ();
Console.log (Instance.getsupervalue ());//true
Prototype inherits JavaScript