最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Arrow function in Express exported class - Unexpected token - Stack Overflow

programmeradmin1浏览0评论

I have file with my class which I exported and use in express and Node. I'ld like to use arrow function and it's an example:

class MyClass {

    myFunc(arg) {
        console.log(arg);
    }

    myArrowFunc = (arg1, arg2) => {
        console.log(arg1);
        console.log(arg2);
    };
}


module.exports = {
    MyClass
}

Everything works fine before adding myArrowFunc. After that I have an error after run app:

myArrowFunc = (arg1, arg2) => {
            ^
SyntaxError: Unexpected token =

My Node version is 9.x so it should work... or I forgot about something.

I have file with my class which I exported and use in express and Node. I'ld like to use arrow function and it's an example:

class MyClass {

    myFunc(arg) {
        console.log(arg);
    }

    myArrowFunc = (arg1, arg2) => {
        console.log(arg1);
        console.log(arg2);
    };
}


module.exports = {
    MyClass
}

Everything works fine before adding myArrowFunc. After that I have an error after run app:

myArrowFunc = (arg1, arg2) => {
            ^
SyntaxError: Unexpected token =

My Node version is 9.x so it should work... or I forgot about something.

Share Improve this question asked Dec 14, 2017 at 15:10 user1408681user1408681 1
  • 1 What's the point of a method that's an arrow function anyway? When you do that, it's not really a method of your object because this won't be the object instance. What problem are you actually trying to solve? – jfriend00 Commented Dec 14, 2017 at 15:15
Add a ment  | 

3 Answers 3

Reset to default 4

You can't put an assignment in a Class like that.

Why not put it in the constructor? You initialize properties in it.

No babeljs needed for that.

class MyClass {
    constructor(){
        this.myArrowFunc = (arg1, arg2) => {
            console.log(arg1);
            console.log(arg2);
        };
    }
    myFunc(arg){
        console.log(arg);
    }
}

Update

This is now possible (Stage 3 proposal / MDN):

class MyClass {
    arrowFunc = (a, b) => {
        console.log(a, b);
    }
    myFunc(arg){
        console.log(arg);
    }
}

Starting with node versions v10.x.x, you can use node --harmony to run code with arrow notation for class methods. Harmony flag enables new ECMAScript 6 features in the language.

In order to use new Javascript (es6) in express app, you need to install babel to pile es6 to es5 code. The Babel piler will allow you to use the new JavaScript features in Express app.

These dependencies are required to use the babel piler in your project.

1) babel-cli: Compile files from the mand line using babel

2) babel-preset-es2015: Babel preset for all es2015 plugins.

Run this mand to install babel in your nodejs project using terminal

npm install -save-dev babel-cli babel-preset-es2015

Rest please follow this link https://www.codementor.io/iykyvic/writing-your-nodejs-apps-using-es6-6dh0edw2o

发布评论

评论列表(0)

  1. 暂无评论