Skip to content
Advertisement

Tag: class

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

What’s the purpose of using classes in React?

I mostly see JavaScript use classes as a constructor as following: What’s the reason React uses classes without using the contructor() function, such as following? I don’t see classes being used to create instances. Answer What’s the reason React uses classes without using the contructor() function From the JavaScript class doc: If you do not specify a constructor method, a

What is the difference between class fields and properties in Javascript

I’m reading class fields proposal for JavaScript. I don’t understand why the authors call it ‘fields’ and not properties. MDN docs in class article speak about instance properties declared inside constructor and in next section about field declarations declared using new syntax. What is the difference between the two besides the syntax? Answer What is the difference between the two

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

Using `this` for Parent Function inside a Nested Function

In the Apple “classs”, how can the nested function countSeeds() retrieve the value this.price? jsfiddle: http://jsfiddle.net/VGqEa/ Output Answer put var self = this at the top of Apple, and then refer to this as self instead in the nested function. i.e.: You could also put the self=this statement at the beginning of this.cutOpen, since this will still refer to the

Advertisement