es5中的定义类
function Box(a,b) { }
Box.prototype={
a:1,
c:function () {
}
};
Object.defineProperty(Box.prototype,"b",{
value:2
});
Box.prototype.d=function () {
};
//指向构造函数
// 对象.constructor 就可以知道当前的对象是谁的实例化内容
Box.prototype.constructor=Box;
//实例化一个box
var box=new Box();
给对象中添加原型属性和继承方法
封装的js文件
(function () {
//添加原型属性方法(深复制)
Object.prototype.addProto=function (sourceObj) {
var names=Object.getOwnPropertyNames(sourceObj);
for(var i=0;i<names.length;i++){
var desc=Object.getOwnPropertyDescriptor(sourceObj,names[i]);
if(typeof desc.value==="object" && desc.value!==null){
var obj=new desc.value.constructor;
obj.addProto(desc.value);//把obj当成引用对象带入递归函数继续给obj赋值
Object.defineProperty(this,names[i],{
enumerable:desc.enumerable,
writable:desc.writable,
configurable:desc.configurable,
value:obj
});
continue;
}
Object.defineProperty(this,names[i],desc);
}
return this;
};
//继承
Function.prototype.extendClass=function (supClass) {
function F() {}
F.prototype=supClass.prototype;
this.prototype=new F();
this.prototype.constructor=this;
this.supClass=supClass.prototype;
if(supClass.prototype.constructor===Object.prototype.constructor){
supClass.prototype.constructor=supClass;
}
}
})();