Using es6 arrow functions inside class

前端 未结 3 1836
孤独总比滥情好
孤独总比滥情好 2020-12-10 20:57

When i change a function draw(){ //} to draw = () => { // } I am getting an error like \"Uncaught SyntaxError: Unexpected token =\". What may be

3条回答
  •  有刺的猬
    2020-12-10 21:44

    First of all, you probably shouldn't do that. Why? Well, because arrow functions are not semantically the same as regular functions.

    If you register the functions as this.draw = () => {//}, then every instance of your class* will have a duplicate definition of that function which is a waste of space and a misuse of some core language features such as prototype inheritance.

    draw() on the other hand registers that function on the prototype so that definition of the draw function can be shared between all instances and even changed dynamically for ALL instances at the same time.

提交回复
热议问题