Controlling the value of ‘this’ in a jQuery event
You can use jQuery.proxy() with anonymous function, just a little awkward that ‘context’ is the second parameter. $(“#button”).click($.proxy(function () { //use original ‘this’ },this));
You can use jQuery.proxy() with anonymous function, just a little awkward that ‘context’ is the second parameter. $(“#button”).click($.proxy(function () { //use original ‘this’ },this));
Bind the callback function also so that this inside the callback points to the context of the React Component and not the callback function getPosts = () => { $.ajax({ type: ‘get’, url: urlname, success: (data) => { this.setState( { posts: data } ) } }); } or you could use bind like getPosts = … Read more
MDN states: array.forEach(callback[, thisArg]) If a thisArg parameter is provided to forEach, it will be used as the this value for each callback invocation as if callback.call(thisArg, element, index, array) was called. If thisArg is undefined or null, the this value within the function depends on whether the function is in strict mode or not … Read more
this is a keyword in JavaScript that is a property of an execution context. Its main use is in functions and constructors. The rules for this are quite simple (if you stick to best practices). Technical description of this in the specification The ECMAScript standard defines this via the abstract operation (abbreviated AO) ResolveThisBinding: The … Read more
this is always the object the method is called on. However, when passing the method to then(), you are not calling it! The method will be stored somewhere and called from there later. If you want to preserve this, you will have to do it like this: .then(() => this.method2()) or if you have to … Read more
In this case, they are the same. The Class.this syntax is useful when you have a non-static nested class that needs to refer to its outer class’s instance. class Person{ String name; public void setName(String name){ this.name = name; } class Displayer { String getPersonName() { return Person.this.name; } } }
You aren’t attaching the event handler correctly. This line: $(‘.figure’).click(toggleCarousel(this)); …is calling toggleCarousel with this immediately (that’s what the parens will do). What you really want is to pass the function object to .click(): $(‘.figure’).click(toggleCarousel); Update: As @FelixKling pointed out, you’ll also want to pass the target of the event to the downstream functions; it … Read more
Node’s REPL is global. Code from a file is in a “module”, which is really just a function. Your code file turns into something like this very simplified example: var ctx = {}; (function(exports) { // your code console.log(this === global); }).call(ctx, ctx); Notice that it’s executed using .call(), and the this value is set … Read more
There are two methods defined for all functions in JavaScript, call(), and apply(). The function syntax looks like: call( /* object */, /* arguments… */ ); apply(/* object */, /* arguments[] */); What these functions do is call the function they were invoked on, assigning the value of the object parameter to this. var myFunction … Read more
Since you make sure to put your instances.add(this) at the end of the constructor you should IMHO be safe to tell the compiler to simply suppress the warning (*). A warning, by its nature, doesn’t necessarily mean that there’s something wrong, it just requires your attention. If you know what you’re doing you can use … Read more