es6-class

ES6: this within static method

╄→尐↘猪︶ㄣ 提交于 2019-11-28 05:13:47
问题 Let's say I have two ES6 classes like this: class Base { static something() { console.log(this); } } class Derived extends Base { } And then I make a call like this: Derived.something(); Note that I am making a call to a static method defined on the super class via sub class. This does not give me errors. It prints [Function: Derived] So accessing this within a static method seems to work here. I need a common static method for all sub-classes of a super class and I need to be able to know

How do you check the difference between an ECMAScript 6 class and function?

本小妞迷上赌 提交于 2019-11-28 04:41:01
In ECMAScript 6 the typeof of classes is, according to the specification, 'function' . However also according to the specification you are not allowed to call the object created via the class syntax as a normal function call. In other words, you must use the new keyword otherwise a TypeError is thrown. TypeError: Classes can’t be function-called So without using try catch, which would be very ugly and destroy performance, how can you check to see if a function came from the class syntax or from the function syntax? I think the simplest way to check if the function is ES6 class is to check the

Are JavaScript ES6 Classes of any use with asynchronous code bases?

旧巷老猫 提交于 2019-11-28 04:37:55
What can ES6 Classes provide, as a pattern of organization, to asynchronous code. Below is an example with ES7 async/await, can an ES6-class have an asynchronous method, or constructor in ES7? Can I do: class Foo { async constructor() { let res = await getHTML(); this.res = res } } And, if not how should a constructor work that does this? class Foo { constructor() { getHTML().then( function (res) { this.res = res } } } If neither of these patterns work, can a constructor (and moreover classes) in an ES6 class support any form of asynchronicity that operates on the object's state? Or, are they

Serializing an ES6 class object as JSON

心已入冬 提交于 2019-11-28 03:09:15
问题 class MyClass { constructor() { this.foo = 3 } } var myClass = new MyClass() I'd like to serialize myClass object to json. One easy way I can think of is, since every member is actually javascript object (array, etc..) I guess I can maintain a variable to hold the member variables. this.prop.foo = this.foo and so on. I expected to find a toJSON/fromJSON library for class objects since I used them with other languages such as swift/java, but couldn't find one for javascript. Maybe class

ES6 functions, arrow functions and 'this' in an ES6 class [duplicate]

本小妞迷上赌 提交于 2019-11-27 22:35:36
This question already has an answer here: Should I write methods as arrow functions in Angular's class 3 answers Arrow vs classic method in ES6 class 1 answer class App extends Component { constructor(props) { ... } onChange = (e) => this.setState({term: e.target.value}) onSubmit(e){ e.preventDefault(); const api_key = "C1hha1quJAQZf2JUlK"; const url = `http://api.giphy.com/v1/gifs/search?q=${this.state.term}&api_key=${api_key}`; } render() { return ( <div> <form onSubmit={this.onSubmit}> <input value={this.state.term} onChange={this.onChange}/> <button>Search!</button> </form> </div> ); } }

When do I need to call `super` from a constructor?

别说谁变了你拦得住时间么 提交于 2019-11-27 19:00:20
问题 Reading Dr. Axel Rauschmayer's blog on ES6 classes, I understand that a derived class has the following default constructor when none is provided constructor(...args) { super(...args); } I also understand that if I want to use this within a constructor I first need to call super , otherwise this will not yet be initialized (throwing a ReferenceError). constructor(width, height) { this.width = width; // ReferenceError super(width, height); this.height = height; // no error thrown ... } Is the

JSON stringify ES6 class property with getter/setter

↘锁芯ラ 提交于 2019-11-27 16:00:50
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 to stringify an instance of MyClass to send it with a HTTP request. The stringified JSON is an object

Are the es6 classes really semantic sugar?

╄→гoц情女王★ 提交于 2019-11-27 08:32:50
问题 If they are just semantic sugar how can I get the same result of the following es6 scripts in es5? class MyFunc extends Function{} new MyFunc('alert("hi guys")')() and class MyArr extends Array{} var myarr = new MyArr(1,2,3,4) myarr[2] = "a value" myarr.push("an other value") 回答1: No, they are only mostly syntactic sugar. They can do all the things that the class pattern did in ES5, but also more than that. The details of how objects are instantiated, especially in subclasses, was overhauled,

Are JavaScript ES6 Classes of any use with asynchronous code bases?

人走茶凉 提交于 2019-11-27 00:25:50
问题 What can ES6 Classes provide, as a pattern of organization, to asynchronous code. Below is an example with ES7 async/await, can an ES6-class have an asynchronous method, or constructor in ES7? Can I do: class Foo { async constructor() { let res = await getHTML(); this.res = res } } And, if not how should a constructor work that does this? class Foo { constructor() { getHTML().then( function (res) { this.res = res } } } If neither of these patterns work, can a constructor (and moreover classes

ES6 functions, arrow functions and 'this' in an ES6 class [duplicate]

心不动则不痛 提交于 2019-11-26 22:56:44
问题 This question already has answers here : Should I write methods as arrow functions in Angular's class (3 answers) Arrow vs classic method in ES6 class (1 answer) Closed last year . class App extends Component { constructor(props) { ... } onChange = (e) => this.setState({term: e.target.value}) onSubmit(e){ e.preventDefault(); const api_key = "C1hha1quJAQZf2JUlK"; const url = `http://api.giphy.com/v1/gifs/search?q=${this.state.term}&api_key=${api_key}`; } render() { return ( <div> <form