I have the following code: the console.log in count() is defined and working fine, but when i try to use this.dust in buyBook() it returns as undefined. Why is this and how do I fix it? Answer You want to bind the this context for buyBook, or the event handler will redefine the this context. Edit: Also, I think you
Tag: this
How to access an array from the scope of a function
I’m trying to add an element into an array with the push() method but apparently, I can’t access that array from the scope of my function. Here’s a summary of my architecture : I know for sure that the problem come from the array. When executing the code, I have an error telling me that push isn’t a propriety of
Toggle classes with this as an event handler in jQuery
I want to toggle the content class independently when I click the collapsible class button associated with it. I read briefly on using this in event handlers. The way I’ve used it so far, I end up toggling the collapsible class (i.e the button) instead. This is close to what I want but instead of toggling the button I want
How to continue running functions down the chain if an error threw?
So, I should create some functions with chains for validating, example: and function should throw an error if something isn’t comparing. I have a problem, if something throw an error, than it doesn’t continue work, I tried to add try catch, but then tests shows me that it don’t throw an error. I want to pass test with .toThrowError() and
A question regarding jQuery hover(), setInterval scope, and The “this” Problem —
UPDATE — from CertainPerformance: This helped with the scope question; but now I have a follow up – Each image at position i needs to be iterating over a separate index, index which determines the current src of the image at position i. The starting index for each image’s setInterval loop should be its position and then index++ each loop.
ES6 Classes: Bind “this” to nested functions
I have multiple nested functions in a ES6 Class. Now I wonder how I can easily bind this of the class instance to all the subfunctions. I know of… …but it feels like an awkward solution for multiple nested functions. Does anyone know of a more elegant solution? Answer You can use arrow functions. They work in rather a similar
JS call static method from class
I have a class with a static method: Is there an equivalent to this for static methods (i.e. refer to the current class without an instance)? So I don’t have to write the class name: “User”. Answer From MDN documentation Static method calls are made directly on the class and are not callable on instances of the class. Static methods
React: “this” is undefined inside a component function
I want to update loopActive state on toggle, but this object is undefined in the handler. According to the tutorial doc, I this should refer to the component. Am I missing something? Answer ES6 React.Component doesn’t auto bind methods to itself. You need to bind them yourself in constructor. Like this:
Trying to understand the difference between passing ‘this’ vs. a reference to the event
I am trying to understand the differences between passing ‘this’ to a function versus passing a reference to the event itself. I am testing with two separate divs and each has a separate function for mouseover and mouseout events. I pass just ‘this’ to one function and I pass both ‘this’ and ‘e’ to the other. My example is here:
Javascript call() & apply() vs bind()?
I already know that apply and call are similar functions which set this (context of a function). The difference is with the way we send the arguments (manual vs array) Question: But when should I use the bind() method ? jsbin Answer I created this comparison between function objects, function calls, call/apply and bind a while ago: .bind allows you