首页 > 技术文章 > 对象的继承

scrit 2014-11-17 11:01 原文

function inherit(p){
    if (p == null) throw TypeError();    //p是一个对象,但不能是null
    if (Object.create)                   //如果Object.create()存在
        return Object.create(p);         //直接使用它
    var t = typeof(p);                   //否则进行进一步检测
    if (t !== "object" && t !== "function") throw TypeError;
    function f() {};                     //定义一个空构造函数
    f.prototype = p;                     //将其原型属性设置为p
    return new f();                      //使用f()创建p的继承对象
}
View Code

参数为需要继承的对象

推荐阅读