This tag is for questions regarding Classes in Ecmascript 6. The tag is only for classes provided in the Ecmascript version.
Questions tagged [es6-class]
1259 questions
33
votes
3 answers
TypeScript Unexpected token, A constructor, method, accessor or property was expected
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,…

Guido Kleijer
- 559
- 1
- 5
- 7
31
votes
2 answers
Why Jest's toThrow won't work when create an instance of a ES6 class directly in the constructor?
class TestObject {
constructor(value) {
if (value === null || value === undefined) {
throw new Error('Expect a value!');
}
}
}
describe('test the constructor', () => {
test('it works', () => {
expect(() => {
new…

Albert Gao
- 3,653
- 6
- 40
- 69
30
votes
4 answers
Typescript : underscore convention for members
I have a class Email
class Email {
private _from: string;
private _to: Array;
private _subject: string;
}
It'll create an email object something like:
{
_from:'',
_to:'',
_subject:''
}
This seems a little weird to me since I…

Jeson Dias
- 883
- 2
- 11
- 26
30
votes
2 answers
Why is "this" undefined in this class method?
I've tried to search over what seems to be the entire internet, but I'm still vexed by a problem with a JS class I'm writing for a Micro Service (still in learning a bit).
So, I try to call a class method on an instantiated object, and according to…

KarlGdawg
- 351
- 1
- 4
- 9
30
votes
2 answers
Why and when do we need to bind functions and eventHandlers in React?
class SomeClass extends Component{
someEventHandler(event){
}
render(){
return
}
}
I see different versions of ------here------ part.
// 1
return

kukrt
- 2,117
- 3
- 21
- 32
29
votes
3 answers
Returning ES6 Proxy from the ES6 class constructor
I want user to only set specific properties to an object but as the same time that object should be constructed from custom class.
For example
var row = new Row({
name : 'John Doe',
email : 'uhiwarale@gmail.com'
}, Schema);
row can have…

Uday Hiwarale
- 4,028
- 6
- 45
- 48
28
votes
4 answers
How do I export more than one class component with React JS?
I'm new to React and would like to keep all my components in one file. How do I export more than one component and keep them in the same file?
import React, { Component } from "react";
class MyText extends Component {
render() {
return…

Tony Carbetta
- 283
- 1
- 4
- 7
26
votes
2 answers
Testing ES6 class with Jest throws 'not a constructor' error
I found a similar problem here, but there doesn't seem to be an answer.
I'm trying to test an ES6 class using Jest, like so:
// src/myclass.js
export default class MyClass {
constructor(options) {
// currently this is empty while I debug…

UnknownDeveloper
- 445
- 1
- 4
- 10
25
votes
5 answers
how to unmock a single instance method with jest
coming from rspec, i am having trouble understanding mocking with jest. the approach i am trying for, is to automock a class's constructor and all of it's functions, and then unmock them one by one to test only that one function. the only…

brewster
- 4,342
- 6
- 45
- 67
25
votes
1 answer
Error: Minified React error #130
I have the following ReactJs component in file ./MyInput.react.js
import React from 'react';
export default class MyInput extends React.Component {
constructor(props) {
super(props);
this.id = getNextId();
this.onChange =…

Roman
- 19,236
- 15
- 93
- 97
24
votes
5 answers
Extend Javascript promise and resolve or reject it inside constructor
I want to extend native Javascript Promise class with ES6 syntax, and be able to call some asynchronous function inside the subclass constructor. Based on async function result the promise must be either rejected or resolved.
However, two strange…

humkins
- 9,635
- 11
- 57
- 75
24
votes
2 answers
unexpected token = for class properties in node 8.4
running the following code in node (v8.4)
class TodoStore {
todos = [];
get completedTodosCount() {
return this.todos.filter(
todo => todo.completed === true
).length;
}
report() {
if…

Elad Katz
- 7,483
- 5
- 35
- 66
24
votes
2 answers
React-router : how to pass "match" object into a component declared as an ES6 class?
I'am new to react-router (v4) and I was following the tutorial until this little problem :
This code works quite well
class App extends Component {
render() {
return (
-
Cabrinha
- 440
- 1
- 3
- 15
20
votes
3 answers
Why isn't this allowed before super()
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…

user4790067
- 451
- 2
- 6
- 12
19
votes
4 answers
Javascript ES6 - Enums inside classes used outside like a static enum
I'd like to ask if it's possible to add an enum similar to:
STATES = {
WIP: "Work in progress",
ONLINE: "Online",
ONLINE_MODIFIED: "Online, modified",
HIDDEN: "Hidden"
}
inside a Class, and be able to use it in some other file with…

Maxime M.
- 357
- 1
- 4
- 17