js使用原型实现继承与new一个新对象的方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
var object = {
isA:function(aType){
var self = this;
while(self){
if(self == aType)
return true;
self = self.Type;
}
return false;
}
};
function Class(aBaseClass,aClassDefine){
function class_(){
this.Type = aBaseClass;
for(var member in aClassDefine)
this[member] = aClassDefine[member];
};
class_.prototype = aBaseClass;
return new class_();
};
function New(aClass,param){
function new_(){
this.Type = aClass;
if(aClass.Create)
aClass.Create.apply(this,param);
};
new_.prototype=aClass;
return new new_();
};
var Person = Class(object,{
Create:function(name,age){
this.name = name;
this.age = age;
},
SayHello:function(){
alert("hello,i'm " + this.name + "," + this.age + "years old.");
}
});
var Employee = Class(Person,{
Create:function(name,age,salary){
Person.Create.call(this,name,age);
this.salary =salary;
},
ShowMeTheMoney:function(){
alert(this.name + "$" + this.salary);
}
});
var Jack = New(Person,["Jack",22]);
var Lily = New(Employee,["lily",21,1234]);
Jack.SayHello();
//alert(Person.isA(object));
alert(Lily.isA(Person));
//alert(Employee.isA(object));
</script>
</head>
<body>
</body>
</html>