通过原型继承创建新对象

来源:互联网 发布:知乎经典问答搞笑 编辑:程序博客网 时间:2024/06/05 04:10
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();   }