javascript USES prototype for single inheritance

  • 2020-05-09 18:08:47
  • OfStack

1. Use prototype for single inheritance.


// define 1 a A class
function A(){
}
// for A Class calls properties dynamically color, With the method sayColor
A.prototype.color = "blue";
A.prototype.sayColor = function(){
alert(this.color);
};
// To create the 1 a B class
function B(){
}
// let B Inherited from A
B.prototype=new A(); //new Out of the A Is assigned to B The prototype, B Is included in A All the properties and methods defined in .
// Can I inherit it sayColor rewrite .
B.prototype.sayColor=function(){
alert(" rewrite ");
}
var b=new B();
b.color='red';
b.sayColor();


Related articles: