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
19
votes
3 answers

instanceof using ES6 class Inheritance chain doesn't work

Using ES6 class syntax, I'm wondering why the instanceof operator doesn't work for the inheritance chain when there are more than one chain of inheritance? (optional read) How instanceof operator works? In obj instanceof Constructor, the…
abhisekp
  • 4,648
  • 2
  • 30
  • 37
19
votes
9 answers

Create object from class name in JavasScript ECMAScript 6

I want create object factory using ES6 but old-style syntax doesn't work with new. I have next code: export class Column {} export class Sequence {} export class Checkbox {} export class ColumnFactory { constructor() { this.specColumn =…
Serhii Popov
  • 3,326
  • 2
  • 25
  • 36
18
votes
3 answers

JavaScript class property inside vs outside constructor

I'm struggling to understand the difference between defining a property inside vs outside the constructor. In the below example both properties are accessible on the instance in the same way, what is the difference? class Foo { constructor() { …
Max888
  • 3,089
  • 24
  • 55
17
votes
1 answer

Javascript, extending ES6 class setter will inheriting getter

In Javascript, with the following illustration code: class Base { constructor() { this._val = 1 } get val() { return this._val } } class Xtnd extends Base { set val(v) { this._val = v } } let x = new Xtnd(); x.val =…
codechimp
  • 1,509
  • 1
  • 14
  • 21
17
votes
4 answers

How to get Map as an object in javascript ES6?

My Class is using es6 to create an map object at node level, using Map()--"set" function.When class is called i want the map object to get converted to normal json type structure. I am using moongoose to retreive the data from…
Kunal Vashist
  • 2,380
  • 6
  • 30
  • 59
17
votes
1 answer

How to Proxy Custom Element (Web Component)

class A extends HTMLElement { constructor() { super() return new Proxy(this, {}) } } window.customElements.define('a-element', A) How can i Proxy custom element? When i try it: Uncaught…
16
votes
3 answers

Extending type when extending an ES6 class with a TypeScript decorator

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…
15
votes
3 answers

When is it appropriate to use a constructor in REACT?

I understand the concept of constructors in OOP languages like C++. However, I am not entirely sure when to use a constructor in REACT. I do understand that JavaScript is object oriented, but I am not sure what the constructor is actually…
WebDream
  • 153
  • 1
  • 1
  • 5
15
votes
1 answer

Creating custom element without using class keyword

This is actually more a question about the object-orientation model in ES6. However I am going to use the creation of a new custom element as an example. So the new and shiny (as of today) method to create a new custom element is via…
Jeffrey04
  • 6,138
  • 12
  • 45
  • 68
15
votes
1 answer

Get all static getters in a class

Let's say I have this class (which I using like an enum): class Color { static get Red() { return 0; } static get Black() { return 1; } } Is there anything similar to Object.keys to get ['Red', 'Black']? I'm using…
Almis
  • 3,684
  • 2
  • 28
  • 58
14
votes
1 answer

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

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…
Richeve Bebedor
  • 2,138
  • 4
  • 26
  • 40
13
votes
3 answers

Is the constructor still needed in React with autobinding and property initializers

I am refactoring an es6 class based React component that uses the normal constructor, and then binds methods, and defines state/attributes within that constructor. Something like this: class MySpecialComponent extends React.Component { …
Max Millington
  • 4,378
  • 4
  • 27
  • 34
13
votes
2 answers

why do functional component in reactjs not have instances?

In React quickstart, it is stated about Refs and Functional Components that You may not use the ref attribute on functional components because they don't have instances: function MyFunctionalComponent() { return ; } class Parent…
thor
  • 21,418
  • 31
  • 87
  • 173
13
votes
2 answers

Cannot call a method within a class it defined it in ES6 in Node.js

I am making an app using Node.js, Express.js and MongoDB. I am using a MVC pattern and also have separate file for routes. I am trying me make a Controller class, in which a method calls another method declared within it. But I cannot seem to be…
Kucl Stha
  • 565
  • 1
  • 6
  • 20
13
votes
4 answers

javascript 'this' in ES6 classes return undefined

I think this is a scope problem, but I'm not sure how to fix this. Here is my code: http://jsfiddle.net/9k9Pe/1498/ class FrameCreator{ constructor(){ this.createFrame(); } createFrame(){ var iframe =…
Doua Beri
  • 10,612
  • 18
  • 89
  • 138
1 2
3
83 84