prototype and constructor object properties

前端 未结 4 1728
孤城傲影
孤城傲影 2020-12-04 14:48

I\'ve:

function Obj1(param)
{
    this.test1 = param || 1;

}

function Obj2(param, par)
{
    this.test2 = param;

}

now when I do:

<
4条回答
  •  攒了一身酷
    2020-12-04 15:19

    Check out Tom Trenka's OOP woth ECMAscript, the "Inheritance" page. Everything from the prototype is inherited, including the constructor property. Thus, we have to unbreak it ourselves:

    Obj2.prototype = new Obj1(42);
    Obj2.prototype.constructor = Obj2;
    

提交回复
热议问题