“this” keyword in event methods when using JavaScript prototype object
You need: this.link.onclick = this.EventMethod.bind(this); …’bind’ is part of Prototype, and returns a function which calls your method with ‘this’ set correctly.
You need: this.link.onclick = this.EventMethod.bind(this); …’bind’ is part of Prototype, and returns a function which calls your method with ‘this’ set correctly.
Both of those examples use an arrow function () => { }, which binds this to a context different from the Vue instance. As per the documentation: Don’t use arrow functions on an instance property or callback (e.g. vm.$watch(‘a’, newVal => this.myMethod())). As arrow functions are bound to the parent context, this will not be … Read more
The this keyword is primarily used in three situations. The first and most common is in setter methods to disambiguate variable references. The second is when there is a need to pass the current class instance as an argument to a method of another object. The third is as a way to call alternate constructors … Read more
Short answer: this points at the nearest bound this – in the code provided this is found in the enclosing scope. Longer answer: Arrow functions do not have this, arguments or other special names bound at all – when the object is being created the name this is found in the enclosing scope, not the … Read more
Array.prototype.map() takes a second argument to set what this refers to in the mapping function, so pass this as the second argument to preserve the current context: someList.map(function(item) { … }, this) Alternatively, you can use an ES6 arrow function to automatically preserve the current this context: someList.map((item) => { … })
You have a few options here, each with its own trade-offs. Unfortunately there is no obvious best solution and it will really depend on the application. Automatic Class Binding As shown in your question: class DemonstrateScopingProblems { private status = “blah”; public run = () => { alert(this.status); } } Good/bad: This creates an additional … Read more
In JavaScript the this object is really based on how you make your function calls. In general there are three ways to setup the this object: someThing.someFunction(arg1, arg2, argN) someFunction.call(someThing, arg1, arg2, argN) someFunction.apply(someThing, [arg1, arg2, argN]) In all of the above examples the this object will be someThing. Calling a function without a leading … Read more
Why is JavaScript bind() necessary? The value of this is determined by how a function is called. If it is you who calls the function then there is usually no need to use .bind, since you have control over how to call the function, and therefore its this value. However, often it is not you … Read more
C++ answer (general answer) Consider a template class Derived with a template base class: template <typename T> class Base { public: int d; }; template <typename T> class Derived : public Base<T> { void f () { this->d = 0; } }; this has type Derived<T>, a type which depends on T. So this has … Read more
Usually, you do not have to, this-> is implied. Sometimes, there is a name ambiguity, where it can be used to disambiguate class members and local variables. However, here is a completely different case where this-> is explicitly required. Consider the following code: template<class T> struct A { int i; }; template<class T> struct B … Read more