Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best approach to avoid javascript's "this" mistakes

In some cases, the this keyword may not refer to the object I expect it to. (recent example: in an key event, in my XBL)

What's the best approach to avoid this kind of mistake?

For now, I'm using always the $.fn from jQuery to store my variables, but I'm not sure if it's the best approach.

like image 770
The Student Avatar asked Dec 23 '10 15:12

The Student


People also ask

What is the most common error in JavaScript?

TypeError is one of the most common errors in JavaScript apps. This error is created when some value doesn't turn out to be of a particular expected type. Some of the common cases when it occurs are: Invoking objects that are not methods.

Why do I keep getting JavaScript errors?

Grammatical mistakes, such as missing parentheses or unmatched brackets, are the major causes of syntax errors in JavaScript. For example, when you must use conditional statements to address multiple conditions, you may miss providing the parentheses as required, leading to syntax flaws.


1 Answers

Learn how and why this behaves the way it does, then read the code you're working on.

Don't trust some magic functionality, you might always end up with unexpected results if you don't know/read the code.

There's simply no single awesome solution for this "problem".

like image 128
Ivo Wetzel Avatar answered Sep 30 '22 06:09

Ivo Wetzel