es6-class

JSON stringify ES6 class property with getter/setter

萝らか妹 提交于 2019-11-26 17:22:56
问题 I have a JavaScript ES6 class that has a property set with set and accessed with get functions. It is also a constructor parameter so the class can be instantiated with said property. class MyClass { constructor(property) { this.property = property } set property(prop) { // Some validation etc. this._property = prop } get property() { return this._property } } I use _property to escape the JS gotcha of using get/set that results in an infinite loop if I set directly to property . Now I need

Getting a list of statics on an ES6 class

。_饼干妹妹 提交于 2019-11-26 10:03:40
问题 Given an ES6 class, how can I inspect it to determine its gettable static properties and methods? In ES5 determining the statics attached to a class (it\'s constructor) was as simple as iterating over the properties of the function. In ES6, is appears there is some magic going on that doesn\'t expose them as such. 回答1: Yes, all methods of class es are non-enumerable by default. You still can iterate them using Object.getOwnPropertyNames. Filter out .prototype , .name and .length (or just

Call static methods from regular ES6 class methods

和自甴很熟 提交于 2019-11-26 00:46:56
问题 What\'s the standard way to call static methods? I can think of using constructor or using the name of the class itself, I don\'t like the latter since it doesn\'t feel necessary. Is the former the recommended way, or is there something else? Here\'s a (contrived) example: class SomeObject { constructor(n){ this.n = n; } static print(n){ console.log(n); } printN(){ this.constructor.print(this.n); } } 回答1: Both ways are viable, but they do different things when it comes to inheritance with an

Why and when do we need to bind functions and eventHandlers in React?

本秂侑毒 提交于 2019-11-25 23:47:29
问题 class SomeClass extends Component{ someEventHandler(event){ } render(){ return <input onChange={------here------}> } } I see different versions of ------here------ part. // 1 return <input onChange={this.someEventHandler.bind(this)}> // 2 return <input onChange={(event) => { this.someEventHandler(event) }> // 3 return <input onChange={this.someEventHandler}> How are the versions different? Or is it just a matter of preference? Thank you all for answers and comments. All are helpful, and I

Call static methods from regular ES6 class methods

♀尐吖头ヾ 提交于 2019-11-25 20:50:49
What's the standard way to call static methods? I can think of using constructor or using the name of the class itself, I don't like the latter since it doesn't feel necessary. Is the former the recommended way, or is there something else? Here's a (contrived) example: class SomeObject { constructor(n){ this.n = n; } static print(n){ console.log(n); } printN(){ this.constructor.print(this.n); } } Bergi Both ways are viable, but they do different things when it comes to inheritance with an overridden static method. Choose the one whose behavior you expect: class Super { static whoami() { return