美文网首页
构造对象的三种方式

构造对象的三种方式

作者: 沈墨空 | 来源:发表于2017-03-19 22:21 被阅读0次

    1.对象字面量

    var a = {
      name: 'Jack'
    }
    

    其prototype指向Object.prototype

    2.构造函数

    function A(){};
    var a = new A();
    

    等价于

    function A(){};
    
    var a = {};
    a.__proto__ = A.prototype;
    A.call(a);
    

    其prototype指向构造函数的prototype指向的对象

    3.Object.create

    var a = {
      name: 'Jack'
    }
    var b = Object.create(a);
    

    其prototype指向a

    参考:https://www.zhihu.com/question/34183746/answer/58068402

    相关文章

      网友评论

          本文标题:构造对象的三种方式

          本文链接:https://www.haomeiwen.com/subject/tevvnttx.html