I get confused about 'this' keyword in the following codes, there are two 'this':
var Foo = function(string){
this.name=string // 1st-this
}
Foo.prototype.get_name = function(){
return this.name // 2nd-this
}
var myFoo = new Foo('John')
the_name=myFoo.get_name()
'the_name' is equal to 'John', the prototype method get the name by return this.name. But can anyone explain to me the 1st-this and 2nd-this, what do they stand for?
this
refers to the object invoking the function, which in this case ismyFoo
. When you constructmyFoo
as anew Foo('John')
thethis
keyword enables you to setmyFoo.name = 'John'
, so when you callmyFoo.get_name()
it will also let youreturn myFoo.name
which equalsJohn
.In your example they are the same, when you call
this
from a method you override in the prototype chain you are referring to the same thing as when callingthis
from within the constructor function.It gets trickier when working with callbacks or when defining member variables in prototype methods.
wow all of these different answers, and only one of them even used the word "context"! Here is the straightforward answer to your question:
the
this
keyword is an object reference that points to the context object.In your example,
this
refers to an instance of theFoo
object.In Javascript, the value of
this
is dependent on the way you call the function.There are 5 ways to call a function in JS, and they all have effect on
this
:new Foo();
<= here, you’re creating a new object, andthis
will reflect that new objectFoo();
<= here, you're calling the function as-is, andthis
will be the global object(!)var obj = { foo: Foo };
<= here, you're calling the function as a method ofobj.foo();
obj
;this
will beobj
Foo.call(thisObject, arg1, arg2);
<= here, you can specify the value ofthis
in the first argumentFoo.apply(thisObject, [args]);
<= here, you can specify the value ofthis
in the first argumentIn 4 and 5, the difference between call and apply is that with
call
, you need to pass all the arguments separately, whereas withapply
, you can pass an array containing all the arguments.Note that in my example 2 above, the function should have been called
foo
instead ofFoo
. Since it’s impossible to know off-hand whether a function is supposed to be called withnew
or not, the consensus is to start the function name with a capital letter if it’s a constructor (and should be used withnew
); otherwise, it should start with lowercase.When you use the
new
keywordthis
is the instance object that you are creating.i.e. the instance of
Bar
being assigned tofoo
When you don't,
this
is the object on which the method you are calling lives.i.e.
foo
in the first example andwindow
in the second (window
is the default object).You can also fritz with it using
apply
Here
this
(still inside thething
method) isbar
)The "this" keyword always refers to the owner of the function. So for instance if you click on a button that accesses that function, this refers to that button. So if you have button A that has an onclick that calls that function then this is A.
In this case this would be John since in both cases they reference myFoo which is John since Myfoo is used in calling them