标签:ar io sp for on log bs cti ad
var object = { //定义object基本类,用于实现最基础的方法和属性
isA: function(type){
var self = this;
while(self){
if(self == type){
return true;
}
self = self.Type;
};
return false;
},
name: ‘object‘
}
function Class(baseClass, defineClass){ //创建类的函数,用于声明类及继承的关系
function _class(){ //创建类的临时函数壳
this.Type = baseClass; //给每个类约定一个Type属性,引用其继承的类
for(var member in defineClass){
this[member] = defineClass[member]; //复制类的全部成员到当前定义的类
}
}
_class.prototype = baseClass;
return new _class();
}
function NEW(Class, param){ //创建对象的函数,用于任意类的对象创建
function _new(){ //创建对象的临时函数壳
this.Type = Class; //给每个对象约定一个Type属性,可以访问到该对象所属的类
if(Class.create){
Class.create.apply(this, param); //约定每个类的构造函数都叫create
}
}
_new.prototype = Class;
return new _new();
}
var person = Class(object, { //person类从object类继承并添加自己的属性和方法
create: function(name, age){
this.name = name;
this.age = age;
},
sayName: function(){
console.log(this.name);
},
sayAge: function(){
console.log(this.age);
}
});
var employee = Class(person, { //employee类从person类继承并添加自己的属性和方法
create: function(name, age, sex){
person.create.call(this, name, age);
this.sex = sex;
},
saySex: function(){
console.log(this.sex);
}
})
var man1 = NEW(person, [‘ly‘, 12]);
var man2 = NEW(employee, [‘wade‘, 12, ‘男‘]);
man1.sayName(); //ly
man2.sayName(); //wade
man2.saySex(); //男
console.log(person.isA(employee)) //false
console.log(employee.isA(person)) //true
function createClass(baseClass, defineClass){
function _createClass(){
for(member in defineClass){
this[member] = defineClass[member];
}
}
_createClass.prototype = baseClass;
return new _createClass();
}
function NEW(aClass, parame){
function _NEW(){
aClass.create.apply(this, parame);
}
_NEW.prototype = aClass;
return new _NEW();
}
var animal = createClass(Object, {
create: function(type){
this.type = type;
},
whoI: function(){
console.log(this.type);
}
});
var bird = createClass(animal, {
create: function(type, name, age){
animal.create.call(this, type);
this.name = name;
this.age = age;
},
isFly: function(){
console.log("I‘m flying");
},
sayAge: function(){
console.log(this.age);
},
sayName: function(){
console.log(this.name);
}
})
var bird1 =NEW(bird, [‘鸟‘,‘老鹰‘,12]);
console.log(bird1);
bird1.sayName();
bird1.sayAge();
bird1.isFly();
bird1.whoI();
标签:ar io sp for on log bs cti ad
原文地址:http://my.oschina.net/u/1992917/blog/357714