I’m expecting to see the value of 26 returned to the console based on the following code snippet, but I get ‘undefined’. Have I used the ‘this’ keyword incorrectly? Answer I change your code a little bit. it should be work now. when you call the function you must need to use function name.
Tag: this
Access React state of function enclosed inside useRef doesn’t update
I’m trying to access React state inside a function enclosed inside of useRef. However, even with a helper function bound to App to access the state, the state never updates inside of the useRef function. Answer The argument passed to useRef is only considered when the component mounts. Only at that time is the value assigned to the ref; it
How to reconcile ‘this’ referencing DOM element and ‘this’ referencing class?
I’m having an issue understanding how I might go about using the this keyword to refer to a DOM element that is has been clicked, but also using that inside a class where this usually refers to the instance of that class. Here’s a snippet of the relevant code: The issue I’m having is that it doesn’t recognize columnController() as
Showing class property value on click
I need to display the this.name in the console when clicking the button element. I could replace inside clicked() and set console.log(myBtn.name) but I’d like this method to work with any object. Is there a way to do it? When I click I get an empty string. Thanks! Answer You was almost there The problem in your code is what
Pass an object’s method to a function in Javascript. Is this the correct interpretation of the question?
I was in an interview, and there was this question: When the method X of an object O is passed to a function Y as a parameter, what happens if X contains a reference to ‘this’ and gets executed inside Y? Please provide code examples. Is this code a correct interpretation of the question? Could you please help me understand
Proper way of writing $(this).text().match(r) into vanilla JS?
I have the following function written in jQuery which I would like to convert to javascript but I couldn’t find a proper way so far. I rewrote it this way: Answer
What is the lexical environment inside a callback function?
I keep hearing that arrow functions inherit the value of this from their Lexical Environment. Consider this example: Why is the value of this inside the arrow callback functions undefined (or in non-strict mode: window)? If the callback function is using the value of this from its lexical environment, shouldn’t the lexical environment be addEventListener? Answer When you call a
Javascript: Create Read More / Read Less Functionality for Blog Posts
Posted this earlier, but marked a reply as an “Answer” before realizing a limitation that would not work with my code. Reposting my question. New to JS. I am creating a blog from scratch, and trying to implement a Read More / Read Less button. I was able to get this working on one blog post, but it will be
How to always make “this” keyword to reference the parent class (bind child methods to parent class)?
here is the simplest form of my problem: I know I can fn.bind(service1) in the mapper function to solve the problem, but as fn is dynamic, I would prefer not to do that. I have tried searching on how to get the parent class from child method but get no results. I want mapper to be able to call a
In javascript, what’s the difference between an instance function and instance variable of type Function?
I know one of the difference is that instance variables of type Function automatically bind to the class. For example: Why is this and are there other difference between these two ways of writing an instance function? Answer You can check what these ways are acting on the Dog.prototype object: Method definition: Public class field [MDN]: In the first case