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
Tag: this
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, …
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: class Service1 { constructor() { this.name = ‘service1’ } getThisName() { console.log(‘Name: ‘ + (this && this.name)) } } const service1 = new …
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: class Dog { sound = ‘woof’ bark() { console.log(this) } boundBark …
variable defined in one function and undefined in other typescript
I have the following code: class Currency { private counter = document.getElementById(‘counter’); private dust = 0; private books = 0; private bookCounter = document.getElementById(&…
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 : class Test{ myArray; …
How to continue running functions down the chain if an error threw?
So, I should create some functions with chains for validating, example: isValid(‘Test string’).required().isString().min(5); and function should throw an error if something isn’t comparing. I have a …