views:

253

answers:

7

In Javascript OO, when should I use the this keyword?

Also, if I want to call a method of a class from another method of the same class, should I use this or just the name of the function? E.g is this correct?

function Foo()
{
   this.bar= function()
   {
      alert('bar');
   }

   this.baz= function()
   {
     this.bar(); //should I use this.bar() or just bar()?
   }
}
A: 

In this particular instance, it's best to use a self-referencing variable in the place of this to prevent confusion and headaches inside functions.

function Foo()
{
   var self = this;

   this.bar= function()
   {
      alert('bar');
   }

   this.baz= function()
   {
     self.bar();
   }
}

The reason for it is because since everything in javascript is an object, the this keyword inside a function refers to the parent function. By defining a variable at a certain scope, your guaranteed that variable will maintain its scope.

tj111
What are the reasons?
Click Upvote
You do it so the scope is obvious. A lot of people use "that" instead of "self."
Nosredna
Or you can do $this.
Artem Barger
Note that by doing so, you're removing the possibility to rebind the function, i.e. `fooInstance.baz.call(barInstance);`
Blixt
@tj111: You said, "In your example, the call to this.bar() is referring to Foo.baz.bar, not Foo.bar". This is not true. If you create an instance of Foo and call baz then the context for baz is your Foo instance. Check it out: http://jsbin.com/onida/edit
Prestaul
A: 

You should use 'this'.

Michael Morton
That's true for the example given, but your answer doesn't really give any useful information.
harto
A: 

Just to stress and empathize previous answer of @tj111 I suggest you to read this. To better understand function scoping.

Artem Barger
+2  A: 

When it comes to "object-oriented" JavaScript, here's a nice guide Mark Dickinson here on SO linked to: Private Members in JavaScript. It does go into detail about some other stuff you don't really need now, but once you understand how JavaScript works, you'll see that it's quite different from your ordinary object-oriented language when it comes to things like what this really means.

I'd say that in your case, you should definitely use this, but maybe your functions should be in the prototype part of your "class" (this avoids redefining the function every time a new instance is created.)

Blixt
I made a simple template for classes that seems to be very similar to what Douglas Crawford has in his article (that I linked.) If looking at code helps, you could always look at that: http://blixt.org/js/classes.js (raw file) http://blixt.org/js#project/js-classes (with syntax highlight)
Blixt
what do you mean by the 'prototype part of the class'?
Click Upvote
Functions have a `prototype` property that is used as the prototype for objects created when calling the function with the `new` keyword. If you set `Foo.prototype.bar = function () {...};`, then all objects created with `new Foo()` will have a `bar` property that references the `Foo.prototype.bar` function.
Blixt
A: 

The correct version is the one that doesn't give an error when you try to call the function. If you omit this you will get a ReferenceError exception.

sth
A: 

I have found 3 Ways to define a javascript class helpful.

Click Upvote
A: 

In another post about 'function aliasing' in JavaScript, I explained in detail, with examples, how 'this' works in JavaScript. I think it may be useful to you.

Please check: http://stackoverflow.com/questions/1007340/javascript-function-aliasing-doesnt-seem-to-work/1162192#1162192

SolutionYogi