0

Am just a beginner in JavaScript and I found this keyword really difficult to understand for me. I know this depends on how the function is invoked.

The code is .

MyClass = function() {
    this.element = $('#element');

    this.myValue = 'something';

    // some more code
}

MyClass.prototype.myfunc = function() {
    this.element.click(function() {

    });
}

new MyClass();

I just need to know what this denotes in this.element.click(function() {}

Does it denote Myclass? Please help me in understanding the use of this keyword in prototype functions in JavaScript.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • `Myclass` is a constructor. When you create a new object from it using `new`, `this` refers to that new object. – Andy Apr 11 '14 at 09:34
  • Brother there are no more links to refer am not geting its informarion properly..:( – user3517846 Apr 11 '14 at 09:34
  • You can check the right side(Related column) of this particular question. http://stackoverflow.com/questions/310870/use-of-prototype-vs-this-in-javascript?rq=1 – Roy M J Apr 11 '14 at 09:35
  • Using a debugger, setting a break point and examining the value will give empiric insight. For the theory read a good book on JavaScript. – mvw Apr 11 '14 at 09:36
  • http://jsbooks.revolunet.com/ – Andy Apr 11 '14 at 09:36
  • @Andy i got it and the thing which T.J crowder answered is applicable only in the case of prototypes ..right ? – user3517846 Apr 11 '14 at 09:42
  • @user3517846: No, it also applies to functions you assign directly to instances: `var o = {}; o.foo = function() { console.log(this.msg); }; o.msg = "Hi there"; o.foo();` gives you `"Hi there"` because during the call to `foo`, `this` is `o`. It's nothing to do with prototypes, in fact; it has to do with how you call the function. See the links in my answer for the full details. – T.J. Crowder Apr 11 '14 at 09:46
  • For more info on what this is you can check this: http://stackoverflow.com/a/16063711/1641941 under "the this variable" Since it's used in prototype.myfunc it doesn't have to be what you expect it to be. Even in constructor functions it doesn't have to be the current object being constructed (like when you forget the new keyword). – HMR Apr 11 '14 at 10:21

1 Answers1

2

I just need to know what this denotes in this.element.click(function() {}

Most likely, it's the object created by a new MyClass() expression. When you do new MyClass(), the JavaScript engine creates a new object, assigns MyClass.prototype as the object's underlying prototype, and then calls MyClass with this pointing to the new object. It returns that object as the result of the expression:

var o = new MyClass();

At this point, if you do:

o.myfunc();

...then within myfunc, this will be equal to o.

this is a slippery concept in JavaScript, though, which is why I said "most likely" above. More (on my blog):

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • so if i call var b = Myclass()..Then this keyword in the function will be b ..right ? – user3517846 Apr 11 '14 at 09:36
  • 1
    @user3517846: No, you need the `new` in there: `var b = new MyClass();` Then, `this` will be `b` within the call, provided you call `myfunc` like this: `b.myfunc();` – T.J. Crowder Apr 11 '14 at 09:37
  • Got it and thanks ..:D ..:D ..and this is applicable only in the case of prototypes right ? – user3517846 Apr 11 '14 at 09:39