vuvu vuvu - 9 days ago 6
Javascript Question

es6 declaring an function with brackets

I want to declare the following function with the short synthax, but I alwys get back a mistake, saying "VM831:9 Uncaught TypeError: Person is not a constructor(…)" Do I need to write function?

var Person = () => {
this.age = 0;

this.set = () => {
this.age++;
}
}

var p = new Person();

Answer

Arrow functions use "lexical" this, i.e. they always inherit the this from the current scope rather than the one created by the new operator.

They therefore cannot be used as constructors.

Comments