this

I am confused about the keyword 'this' in JavaScript

半世苍凉 提交于 2019-11-30 15:01:37
This is an example: function one() { var a = 1; two(); function two() { var b = 2; three(); function three() { var c = 3; alert(a + b + c); // 6 } } } one()​; //calling the function Now when we call function one(), the result is 6 . So it's all about scope chain, all variables are resolved, now I have one question. Why do we need this " this " keyword when all variables are getting resolved through scope chain? So if we have the following function: function a() { var a = 'function a'; function b() { var b = 'function b'; alert (a); //will be function a, without keyword this alert (this.a); //

Please explain “this” to me

自闭症网瘾萝莉.ら 提交于 2019-11-30 14:34:49
问题 I've read hundreds of explanations on "this" in java and I'm really having trouble grasping it. I'm learning android and java side-by-side, I know it's harder that way but I'm enjoying it. The one thing I'm getting killed on is "this"... I'm pasting code from a tutorial below that utilizes "this" one time. I was going to just put a piece of the code but want to be as helpful as possible. I'm looking for a good explanation of "this" that I can add to my notes. Any and all help is appreciated.

“this” keyword: Working mechanism in Java

ⅰ亾dé卋堺 提交于 2019-11-30 13:14:23
问题 After learning Java for sometime, its the first time the use of this keyword has confused me so much. Here is how I got confused. I wrote the following code: class BasicInheritanceTest3Base{ private int x = 0; public int y; public void a() { x++; this.x++; System.out.println("BasicInheritanceTest3Base.a()"); b(); this.b(); System.out.println(x); System.out.println(y); } public void b(){ System.out.println("BasicInheritanceTest3Base.b()"); } } public class BasicInheritanceTest3 extends

How does JavaScript determine when to give a function call a “this” context? [duplicate]

前提是你 提交于 2019-11-30 11:41:59
This question already has an answer here: Cases where 'this' is the global Object in Javascript 4 answers For obvious reasons, in JavaScript, the following two calls are different: foo.bar(); var bar = foo.bar; bar(); Namely, in the first call, this is the foo object. In the second, it's a reference to the global scope. However, the following example is a little less intuitive: (foo.bar)(); I would expect it to operate the same way as the second example, but it actually operates the same as the first. That is, this references foo , not the global scope. What are JavaScript's rules for deciding

address of this

故事扮演 提交于 2019-11-30 08:54:20
I try to find address of this pointer, but this code is showing a strange error: #include <iostream> using namespace std; class Base { public: void test() { void *address_of_this =&this; cout<<address_of_this<<endl; } }; int main() { Base k; k.test(); return 0; } //error non-lvalue in unary'&' Can you explain this error ? Also point that what is illegal in taking address of this ? this is a pointer containing the address to the "current object". It is not a variable that is stored somewhere (or could even be changed), it is a special keyword with these properties. As such, taking its address

Accessing this from within an object's inline function

北慕城南 提交于 2019-11-30 08:27:42
I'm having difficulty referencing "this" from within a javascript inline function, within an object method. var testObject = { oThis : this, testVariable : "somestring", init : function(){ console.log(this.testVariable); // outputs testVariable as expected this.testObject.submit(function(){ var anotherThis = this; console.log(this.testVariable) // undefined console.log(oThis.testVariable) // undefined console.log(testObject.testVariable) // outputs testVariable console.log(anotherThis.testVariable) // undefined } } How do I access this.testVariable from within the submit function? I'm also

C++ using this pointer in constructors

江枫思渺然 提交于 2019-11-30 07:20:38
问题 In C++ , during a class constructor, I started a new thread with this pointer as a parameter which will be used in the thread extensively (say, calling member functions). Is that a bad thing to do? Why and what are the consequences? My thread start process is at the end of the constructor. 回答1: The consequence is that the thread can start and code will start executing a not yet fully initialized object. Which is bad enough in itself. If you are considering that 'well, it will be the last

“this” keyword: Working mechanism in Java

若如初见. 提交于 2019-11-30 06:49:16
After learning Java for sometime, its the first time the use of this keyword has confused me so much. Here is how I got confused. I wrote the following code: class BasicInheritanceTest3Base{ private int x = 0; public int y; public void a() { x++; this.x++; System.out.println("BasicInheritanceTest3Base.a()"); b(); this.b(); System.out.println(x); System.out.println(y); } public void b(){ System.out.println("BasicInheritanceTest3Base.b()"); } } public class BasicInheritanceTest3 extends BasicInheritanceTest3Base { private int x = 3; public int y = 2; public void b() { System.out.println(

How can you assign a value to the pointer 'this' in C++

好久不见. 提交于 2019-11-30 06:42:05
In a function, how to you assign this a new value? Luchian Grigore You can't. 9.3.2 The this pointer [class.this] 1 In the body of a non-static (9.3) member function, the keyword this is a prvalue expression whose value is the address of the object for which the function is called. [...] (emphasis & link mine) You can modify the object this points to, which is *this . For example: struct X { int x; void foo() { this->x =3; } }; The method modifies the object itself, but something like this = new X is illegal. You can assign the object this points at: *this = XY; But you can't assign the direct

Why declare properties on the prototype for instance variables in JavaScript

爷,独闯天下 提交于 2019-11-30 06:17:11
问题 I'm trying to get my head around this black art called JavaScript - and, I must admit, pretty excited about it. I've been looking at code examples, mainly from "easeljs" since that is what I will be using mainly. And I'm a bit confused.. I (think I) understand the difference between using the prototype for functions or properties that are class variables and using this.someProp for 'instance' variables (Yes, I understand that there are no classes in JavaScript.) The code I have looked at, and