Inheritance in javascript, variables in the “parent”

前端 未结 3 1626
隐瞒了意图╮
隐瞒了意图╮ 2020-12-30 10:45

I am doing OO javascript for the first time. I have read about inheritance and prototype and thought I had cracked it. Until I discovered this little example.



        
3条回答
  •  囚心锁ツ
    2020-12-30 11:33

    I think

    TestObject2.prototype = new TestObject();
    

    is overriding the constructor of TestObject2.

    Try

    function TestObject2(data)
    {
        TestObject.call( this, data);
        this.__construct(data);
        this.dothings = function()
        {
            this.dosomestuff();
        }
    }
    
    
    TestObject2.prototype = new TestObject();
    TestObject2.prototype.constructor = TestObject2;
    

提交回复
热议问题