-2

I'm a beginner javascript student , I have a problem with understanding the main goal of the keyword this and how to use it .

Please can anyone help me with examples.

Thanks in advance.

  • `this.show()` will call method show() if such is implemented in the object. `$len=this.len` will check the len field of object referenced by `this`. It is used to reference the object currently being in context. – mnmnc Apr 04 '13 at 13:32
  • 1
    [JavaScript: The Good Parts](http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742) covers the use of `this` quite thoroughly. – Marty Apr 04 '13 at 22:27
  • ^^ That book is amazing. Highly recommended. – jahroy Apr 04 '13 at 22:47

1 Answers1

0

The this keyword is a reserved keyword that, in general, is bound to an object.

When this appears in the body of a function, the object that is referenced by this usually depends on how the function was called.

There are many subtle details that can be found on MDN.this

Frequent accepted usage that you will encounter (beware, this is not always the case depending on ... the context) :

  • when dealing with object methods, this references the current instance
  • when dealing with DOM events, this references the DOM element in the context of the event
Jerome WAGNER
  • 21,986
  • 8
  • 62
  • 77