Questions tagged [es6-class]

This tag is for questions regarding Classes in Ecmascript 6. The tag is only for classes provided in the Ecmascript version.

Resources

1259 questions
222
votes
3 answers

Call static methods from regular ES6 class methods

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…
simonzack
  • 19,729
  • 13
  • 73
  • 118
146
votes
13 answers

How to clone a javascript ES6 class instance

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…
Tom
  • 17,103
  • 8
  • 67
  • 75
98
votes
5 answers

ES6 Singleton vs Instantiating a Class once

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…
Aaron
  • 2,364
  • 2
  • 31
  • 56
96
votes
5 answers

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

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() { …
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
72
votes
14 answers

Angular: 7.2.1 ES6 class ReferenceError : Cannot access 'X' before initialization

I'm having the following TypeScript class export class Vehicule extends TrackableEntity { vehiculeId: number; constructor() { super(); return super.proxify(this); } } my typescript target in tsconfig.json is configured as…
anon
69
votes
3 answers

How and why would I write a class that extends null?

JavaScript's class syntax, added in ES6, apparently makes it legal to extend null: class foo extends null {} new foo(); Some Googling reveals that it was suggested on ES Discuss that such declarations be made an error; however, other commenters…
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
51
votes
2 answers

Anonymous class instance — is it a bad idea?

In ES6 we can do anonymous class: var entity = class { } But we can also immediately instantiate it: var entity = new class { constructor(name) { this.name = name; } getName() { return this.name; } }('Foo'); console.log(entity.getName());…
Steve Fan
  • 3,019
  • 3
  • 19
  • 29
46
votes
2 answers

how to define a static property in the ES6 classes

I want to have a static property in an ES6 class. This property value is initially an empty array. class Game{ constructor(){ // this.cards = []; } static cards = []; } …
Amir Azarbashi
  • 463
  • 1
  • 4
  • 4
46
votes
8 answers

Serializing an ES6 class object as JSON

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…
eugene
  • 39,839
  • 68
  • 255
  • 489
44
votes
7 answers

How do I use a static variable in ES6 class?

I'm trying to use a static variable in es6. I'd like to declare a static variable count in Animal class and increase it. However, I couldn't declare a static variable through static count = 0;, so I tried another way like this: class Animal { …
Simon Park
  • 694
  • 1
  • 7
  • 18
43
votes
7 answers

JSON stringify ES6 class property with getter/setter

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 =…
Thomas Chia
  • 455
  • 1
  • 4
  • 9
42
votes
10 answers

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

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…
Moncader
  • 3,388
  • 3
  • 22
  • 28
39
votes
3 answers

How to use JSDoc to document an ES6 class property

I'm using the documentation package, but cannot figure out how to get it to document class properties (that aren't defined via getters and setters). As the following just generates class documentation for SomeClass, but omits the someProperty…
balupton
  • 47,113
  • 32
  • 131
  • 182