window.onload = function(){
var a = new Object();
a.x = 1;
a.y = 2;
// var b = inherit({x:4,y:2})
var b = inherit(a);
console.log(b); // object对象
console.log(b.x); // 1
}
// 通过原型继承创建新对象
function inherit(obj){
if(obj == null) throw TypeError();
if(Object.create){
return Object.create(obj);
}
var t = typeof obj;
if(t !== 'object' && t !== "function") throw TypeError();
function newObj(){};
newObj.prototype = obj;
return new newObj();
}
通过原型继承创建新对象
最新推荐文章于 2024-04-18 23:39:11 发布