Js USES the delete implementation to inherit the sample code

  • 2020-03-30 03:01:47
  • OfStack

 
//Object impersonation method to implement js inheritance
function A(color) { 
this.Acolor = color; 
this.AshowColor = function() { 
document.writeln("Acolor: " + this.Acolor); 
} 
} 

function B(color, name) { 
//Assign newMethod to A and call the constructor of A
this.newMethod = A; 
this.newMethod(color); 
//Then remove the reference to A so that he cannot be called later
delete this.newMethod; 

this.Bname = name; 
this.BshowName = function() { 
document.writeln("Bname: " + this.Bname); 
} 
} 

var objA = new A("red"); 
objA.AshowColor(); 
document.writeln("----------------"); 
var objB = new B("black", "demo"); 
objB.AshowColor(); 
objB.BshowName(); 
document.writeln("----------------"); 

Related articles: