es6-class

Why isn't this allowed before super()

这一生的挚爱 提交于 2019-11-30 16:56:11
问题 I have been coding in React js. I have read that in ES6 classes to access 'this' we need to first call super(props) and I would like to know why this is.Answers I have found mainly talk about Javascript being unable to know what 'this' is unless superclass is called. I would like to know what that means because outside the constructor, 'this' is recognized and we don't call super(props) each time. class MyComponent extends React.Component { constructor(props) { super(props); this.state = { /*

What's the recommended way to customize toString? Using Symbol.toStringTag or overriding toString?

狂风中的少年 提交于 2019-11-30 09:22:00
I'm confused on what to implement, first, my module will use Babel so there are no problems implementing ES6 features, second, I will use the class construct to create class and not the old prototypal method. So now, I'm confused if I'm going to override toString (which is the old way) or just implement Symbol.toStringTag like what this MDN documentation said, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag So what is the recommended way? They're for different things entirely. If you're trying to define the string version of your object,

TypeScript Unexpected token, A constructor, method, accessor or property was expected

你离开我真会死。 提交于 2019-11-30 03:37:03
问题 Just trying to write a function within a class using typescript. class Test { function add(x: number, y: number): number { return x + y; } } This results in the following error: TypeScript Unexpected token, A constructor, method, accessor or property was expected. I copied the example from: https://www.typescriptlang.org/docs/handbook/functions.html Am I missing something? I'm confused! 回答1: You shouldn't use the function keyword in a Typescript class definition. Try this instead: class Test

ES6 Singleton vs Instantiating a Class once

╄→гoц情女王★ 提交于 2019-11-29 23:53:05
I see patterns which make use of a singleton pattern using ES6 classes and I am wondering why I would use them as opposed to just instantiating the class at the bottom of the file and exporting the instance. Is there some kind of negative drawback to doing this? For example: ES6 Exporting Instance: import Constants from '../constants'; class _API { constructor() { this.url = Constants.API_URL; } getCities() { return fetch(this.url, { method: 'get' }) .then(response => response.json()); } } const API = new _API(); export default API; Usage: import API from './services/api-service' What is the

How to clone a javascript ES6 class instance

♀尐吖头ヾ 提交于 2019-11-29 22:00:53
How do I clone a Javascript class instance using ES6. I'm not interested in solutions based on jquery or $extend. I've seen quite old discussions of object cloning that suggest that the problem is quite complicated, but with ES6 a very simple solution presents itself - I will put it below and see if people think it is satisfactory. edit: it is being suggested that my question is a duplicate; I saw that answer but it is 7 years old and involves very complicated answers using pre-ES6 js. I'm suggesting that my question, which allows for ES6, has a dramatically simpler solution. let clone =

ES6 javascript class inheritance, why we need call to super() from derived class

耗尽温柔 提交于 2019-11-29 16:48:31
In javascript ES6, in inheritance, if derived class has constructor, why it is mandatory to call super from derived constructor ? few failed examples are -: . Base with constructor but derived not calling super- class Base{constructor(){}} class Derived{constructor(){}} var d = new Derived(); // fails - ReferenceError: this is not defined ...it seems it is mandatory to have constructor function in base class. Not really. If you don't provide one, one will be provided for you by the JavaScript engine . So there will always be one (it's mandatory in that sense), but it doesn't have to be coded

ES6: this within static method

眉间皱痕 提交于 2019-11-29 12:09:25
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 what sub-class is calling this method. Now my question is whether using this within a static method is

ES6 Singleton vs Instantiating a Class once

人盡茶涼 提交于 2019-11-28 17:04:59
问题 I see patterns which make use of a singleton pattern using ES6 classes and I am wondering why I would use them as opposed to just instantiating the class at the bottom of the file and exporting the instance. Is there some kind of negative drawback to doing this? For example: ES6 Exporting Instance: import Constants from '../constants'; class _API { constructor() { this.url = Constants.API_URL; } getCities() { return fetch(this.url, { method: 'get' }) .then(response => response.json()); } }

Are the es6 classes really semantic sugar?

落花浮王杯 提交于 2019-11-28 14:17:10
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") 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, and now allows to subclass the builtins like Function and Array as in your question. This was not possible

Extending type when extending an ES6 class with a TypeScript decorator

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 05:22:47
问题 I am trying to decorate a class with a decorator ( a-la-angular style), and add methods and properties to it. this is my example decorated class: @decorator class Person{ } and this is the decorator: const decorator = (target)=>{ return class New_Class extends target { myProp:string } } but myProp is not a known property of Person: person.myProp //Error - myProp does not exist on type Person How can I decorate a typescript class and preserve type completion, type safety, etc ? 回答1: To