inherit.js

版权声明:欢迎评论、补充;24小时内回复;及时更新;欢迎转载。 https://blog.csdn.net/li905663280/article/details/84705723

6.1 inherit

指定一个对象,返回一个继承原型为该对象的js对象

 function inherit(obj){
        if(obj==null) throw TypeError();
        if(Object.create){
            return Object.create(obj);
        }
        if(typeof obj !=="object" || typeof obj!=="function") throw TypeError();
        function f(){};
        f.prototype=obj;
        return new f();
     }

对象为json

t=inherit({x:1});;

对象为方法

t=inherit(function Person(){});

猜你喜欢

转载自blog.csdn.net/li905663280/article/details/84705723