does javascript's this object refer to newly created object in the way i think

前端 未结 3 610
清歌不尽
清歌不尽 2020-11-28 16:39

So, when we create constructor function for creating new object the new keyword does 3 things I\'am going to explain it but please correct me if I\'am wrong i want to be sur

3条回答
  •  攒了一身酷
    2020-11-28 16:49

    In this example, during constructor invocation, obj1 is undefined. Value is assigned to obj1 variable after ObjectCreate function returns.

    You can check yourself:

    function ObjectCreate(){
        this.a = "a";
        this.b = "b";
    
        alert(obj1); // yields "undefined"
        ObjectCreate.prototype.show = function(){
            alert(this.a+" "+this.b);
        }
    }
    
    var obj1 = new ObjectCreate(); // note "var"
    

提交回复
热议问题