Javascript Object Oriented Inheritance

Javascript object-oriented inheritance
//parent class
function Class() {
this.name = "name";
this.method = function () {
alert("method");
}
}
//subclass
function Class1() {
this.name1 = "name1";
this.method1 = function () {
alert("method1");
}
}
//Subclass inherits parent class
Class1.prototype = new Class();
var obj = new Class1();
alert(obj. name);
alert(obj.name1);
obj.method();
obj.method1();
/****** Subclass inherits the syntax of parent class, subclass.prototype=new Parent class(); ** ***/
Subclass override parent class
//Subclass
function Class2() {
this.name2 = "name2";
this.method2 = function () {
alert("method2");
}
}
Class2.prototype = new Class(); //Inherit
Class2.prototype.name = "updateName"; //Rewrite the property of the parent class
Class2.prototype .method = function () {//Rewrite the method of the parent class
alert("UpdateMethod");
}
var obj2 = new Class2();
alert(obj2.name); //Display updateName
obj2.method(); // show UpdateMethod
alert(obj2.name2);
obj2.method2();

Guess you like

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