Javascript this object refers to a newly created object the way I think

So, when we create a constructor for creating a new object, the new keyword does 3 things. I will explain it, but please correct me, if I am wrong, I want to be sure that I am right

first i will create a constructor function

function ObjectCreate(){
    this.a = "a";
    this.b = "b";

    ObjectCreate.prototype.show = function(){
        alert(this.a+" "+this.b);
    }
}

obj1 = new ObjectCreate();

now the first thing a new keyword does is create a new object and set its secret link to its prototype constructor and pass it to the constructor function, where it thiscan now refer to it and please note that it thisdoes not apply to obj1 in to this point, because as soon as the constructor has finished creating the object only then, it returns the newly created object of the variable obj1. I ask this question because some people say that they thisrefer to the obj1 object in this case. therefore i am here.

-1
source share
3 answers

obj1 undefined. obj1 ObjectCreate.

:

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"
0

, . -, , . :

function ObjectCreate(){
   this.a = "a";
   this.b = "b";
}

ObjectCreate.prototype.show = function(){
     alert(this.a+" "+this.b);
}

obj1 = new ObjectCreate();

, .

, this . , , , :

function ObjectCreate(x,y){
   this.a = x*x;
   this.b = y*x+4;
}
obj1 = new ObjectCreate(10,20);

, , this (obj1, ).

, , this , this.a this.b, , , , , vale this.a this.b , .

, . , , .

+2

, , this invoking object.

window.sayHi=function(){
  console.log(this.name);
}
window.name="Window"
window.sayHi();//=Window

var obj={
  name:"obj"
}
obj.fn=window.sayHi;

obj.fn();//=obj

, this . , closure call, apply bind:

//closure example
obj.fn=(function(w){//w is available because the returned function is a closure
  return function(){
    w.sayHi();
  }
}(window));

obj.fn();//=Window
//using call
obj.fn.call(window);//=Window
//using apply
obj.fn.apply(window);//=Window
//using bind
var boundFn=obj.fn.bind(window);
boundFn();//=Window

, . , this , .

, :

var obj={
  name:"obj"
}
var Test=function(){
  this.name="Test";
}
Test.prototype.sayHi=function(){
  console.log(this.name);
};

var t=new Test();
obj.fn=t.sayHi
t.sayHi();//=Test
obj.fn();//=obj

, setTimeout :

someButton.onclick=t.sayHi;//when button is clicked this will be the button clicked
setTimeout(t.sayHi,100);//when sayHi is executed 'this' will be window

To answer your question about obj1 existing in the body of the constructor function; I would say no (not in Firefox, at least). I have no reference to the specifications, but obj1 will matter thiswhen the constructor function returns:

//clean up window.t
delete window.t;
var Test=function(){
  this.name="Test";
  console.log("and window.t is:",window.t);//undefined
}
Test.prototype.sayHi=function(){
  console.log(this.name);
};

window.t=new Test();

Read more about constructor functions, prototype, inheritance, overriding and calling super here .

0
source

All Articles