通过原型继承创建新对象

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Alecor/article/details/75589023

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();
   }

猜你喜欢

转载自blog.csdn.net/Alecor/article/details/75589023