意外的标识符 - 常量

时间:2017-06-17 20:13:51

标签: javascript node.js ecmascript-6

我正在学习node.js并且我在示例中遇到问题:

"use strict";

// Create a new globetrotter!
const globetrotter = {
    // Language spoken in the country our globetrotter is currently in
    const current_lang = undefined,

    // Number of countries our globetrotter has travelled to
    let countries = 0,

    // See how many countries we've travelled to
    get countryCount () {
        return this.countries;
    }, 

    // Reset current language whenever our globe trotter flies somewhere new
    set languages (language) {
        // Increment number of coutnries our globetrotter has travelled to
        countries += 1;

        // Reset current language
        this.current_lang = language; 
    };
};

globetrotter.language = 'Japanese';
globetrotter.countryCount; // 1

globetrotter.language = 'Spanish';
globetrotter.countryCount; // 2

这回复了我的错误:

index.js:6
    const current_lang = undefined,
          ^^^^^^^^^^^^

SyntaxError: Unexpected identifier

为什么以及如何解决此错误?

我在8.1.0版本中有node.js.

1 个答案:

答案 0 :(得分:0)

您在对象文字中定义变量,这不是对象的正确javascript语法。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects