Home > Web Front-end > JS Tutorial > Javascript object-oriented four inheritance_js object-oriented

Javascript object-oriented four inheritance_js object-oriented

WBOY
Release: 2016-05-16 18:10:48
Original
905 people have browsed it
Copy code The code is as follows:

var JsObject = {} || new Object();
JsObject.extend = function(subClass, superClass){
// First determine whether the subclass subClass has been defined. If not, redefine the class.
 if(typeof subClass == "undefined")subClass = function(){};
  //If the parent class superClass is a class, convert it into an object
  if(typeof superClass == "function") superClass = new superClass();
// Traverse the attributes and methods in the superClass object of the parent class
for(var p in superClass)
{
  /* Sum the attributes and methods in the superClass object of the parent class The method is copied to the subclass prototype object,
  Therefore, the subclass has all the characteristics of the parent class, which is inheritance*/ 
subClass.prototype[p] = superClass[p];
}
return subClass;
};
function Student()
{
this.name = "Zhang San";
this.updateName = function(name){
this.name = name ;
}
}
function Class1()
{
this.sex = "Male";
this.updateSex = function(sex){
this.sex = sex;
}
}
//Definition class Class1 inherits the Student class
Class1 = JsObject.extend(Class1, Student);
var obj = new Class1();
alert (obj.sex);
alert(obj.name);
obj.updateSex("女");
obj.updateName("Mary");
alert(obj.sex);
alert(obj.name);

The results show: male, Zhang San, female, Mary
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template