Why was JavaScript designed such that "this" refers to the object invoking a function, rather than the object containing the function invoked?
-
In other programming languages the "this" keyword, or its equivalent, typically refers to the object in which a method is defined. In Javascript "this" can change depending on what calls a function and whether or not you invoke a function using method syntax. It often requires workarounds to get the typical behavior you'd expect. I assume this was a deliberate design decision, but was wondering why it was made, and if it has any benefits. More information (but not an answer as to why) available here: http://stackoverflow.com/questions/80084/in-javascript-why-is-the-this-operator-inconsistent
-
Answer:
I think Alan Storm's reply on SO answers the question, albeit at length. It's useful when you want to use a generic function for multiple objects: var s, c, o1, o2; s = function (newStatus) { this.status = newStatus.toUpperCase() || "NONE"; return this; // allow method chaining, ala jQuery }; c = function () { console.log(this.status); return this; }; o1 = { status: "", setStatus: s, log: c }; o2 = { status: "", update: s writeStatus: c }; o1.setStatus("object 1").log(); // "OBJECT 1" o2.update("hello, world!").writeStatus(); // "HELLO, WORLD!" // Even works on anonymous objects: ({ status: "INIT", say: c }).say(); // "INIT" Regarding the "requires workarounds to get the typical behavior you'd expect", this is only true if you expect it to work like a classical language. JavaScript is not a classical language so getting out of that mindset will make things easier on you :-)
Andy Farrell at Quora Visit the source
Other answers
I wager the reason why is that the functions can be passed around. In most OO languages, the binding of "this" to the object is very much a part of the class structure. Whereas in JavaScript, there is no tight binding between objects and functions.
Jeffrey M. Barber
Related Q & A:
- How To Send a Complex Object to a Rest Web Service?Best solution by codeproject.com
- Why is Javascript called Javascript, if it has nothing to do with Java?Best solution by Stack Overflow
- how to call a function in Python in another function?Best solution by Yahoo! Answers
- Why do most people in the USA build their houses out of wooden material rather than stone in the tornado belt?Best solution by Yahoo! Answers
- Why is it better to buy a snowboard rather then rent?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.