class inheritance

class inheritance

Class inheritance is to call the constructor of the parent class in the function object, so that it can obtain the methods and properties of the parent class. The call and apply methods provide support for class inheritance. By changing the action environment of this, the subclass itself has various properties of the parent class.

var father=function(){
 
  this.age=22;
 
  this.say=function(){
 
    alert('hello i am '+this.name+' and i am '+this.age+'years old');
 
  }
 
}
 
var child=function(){
 
  this.name='wyy';
 
  father.call(this);
 
}
 
var man = new child ();
 
man.say();
 

 

prototypal inheritance

Prototypal inheritance is often used in development. It is different from class inheritance because inheritance is not on the object itself, but on the prototype of the object. Assign the object of the parent class to the prototype of the constructor of the child class, so that the object of the child class can access the properties in the prototype of the parent class and the constructor of the parent class. The prototypal inheritance diagram for this method is as follows:

var Parent = function(){
    this.name = 'parent' ;
} ;
Parent.prototype.getName = function(){
    return this.name ;
} ;
Parent.prototype.obj = {a : 1} ;

var Child = function(){
    this.name = 'child' ;
} ;
Child.prototype = new Parent() ;

var parent = new Parent() ;
var child = new Child() ;

console.log(parent.getName()) ; //parent
console.log(child.getName()) ; //child

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326754567&siteId=291194637