All Questions

684
votes
6answers
183143 views

How to access the correct `this` inside a callback?

I have a constructor function which registers an event handler: function MyConstructor(data, transport) { this.data = data; transport.on('data', function () { alert(this.data);...
912
votes
19answers
290312 views

How does the "this" keyword work?

I have noticed that there doesn't appear to be a clear explanation of what the this keyword is and how it is correctly (and incorrectly) used in JavaScript on the Stack Overflow site. I have witnesse...
671
votes
13answers
96593 views

Use of 'prototype' vs. 'this' in JavaScript?

What's the difference between var A = function () { this.x = function () { //do something }; }; and var A = function () { }; A.prototype.x = function () { //do something };
26
votes
6answers
6238 views

Javascript: Do I need to put this.var for every variable in an object?

In C++, the language I'm most comfortable with, usually one declares an object like this: class foo { public: int bar; int getBar() { return bar; } } Calling getBar() works fine (ignoring t...
916
votes
6answers
246645 views

'this' vs $scope in AngularJS controllers

In the "Create Components" section of AngularJS's homepage, there is this example: controller: function($scope, $element) { var panes = $scope.panes = []; $scope.select = function(pane) { ang...
221
votes
7answers
61184 views

When to call activity context OR application context?

There has been a lot of posting about what these two contexts are.. But I'm still not getting it quite right As I understand it so far: Each is an instance of its class which means that some programm...
481
votes
7answers
487084 views

jQuery: What's the difference between '$(this)' and 'this'?

I am currently working through this tutorial: Getting Started with jQuery For the two examples below: $("#orderedlist").find("li").each(function (i) { $(this).append(" BAM! " + i); }); $("#reset...
163
votes
5answers
114202 views

Pass correct "this" context to setTimeout callback?

How do I pass context into setTimeout? I want to call this.tip.destroy() if this.options.destroyOnHide after 1000 ms. How can I do that? if (this.options.destroyOnHide) { setTimeout(function() {...
290
votes
6answers
88509 views

What does 'var that = this;' mean in JavaScript?

In a JavaScript file I saw: function Somefunction(){ var that = this; ... } What is the purpose of declaring that and assigning it to this?
25
votes
5answers
3136 views

What does "this" refer to in arrow functions in ES6?

I've read in several places that the key difference is that "this is lexically bound in arrow functions." That's all well and good, but I don't actually know what that means. I know it means it's uni...

Previous Next