Preserving a reference to “this” in JavaScript prototype functions [duplicate]

For preserving the context, the bind method is really useful, it’s now part of the recently released ECMAScript 5th Edition Specification, the implementation of this function is simple (only 8 lines long): // The .bind method from Prototype.js if (!Function.prototype.bind) { // check if native implementation available Function.prototype.bind = function(){ var fn = this, args … Read more

What is the ‘global’ object in NodeJS

Value of this in a node module: this in NodeJS global scope is the current module.exports object, not the global object. This is different from a browser where the global scope is the global window object. Consider the following code executed in Node: console.log(this); // logs {} module.exports.foo = 5; console.log(this); // log { foo:5 … Read more

“this” is undefined inside map function Reactjs

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) => { … })

TypeScript “this” scoping issue when called in jquery callback

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

Can “this” ever be null in Java?

No it can’t. If you’re using this, then you’re in the instance so this isn’t null. The JLS says : When used as a primary expression, the keyword this denotes a value that is a reference to the object for which the instance method was invoked (ยง15.12), or to the object being constructed. If you … Read more

Javascript “this” pointer within nested function

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)