简体   繁体   English

声明对象文字时出现意外的标识符错误

[英]unexpected identifier error when declaring object literal

Trying to complete a simple node.js exercise, I have tried several variations on this. 为了完成一个简单的node.js练习,我对此进行了多种尝试。 I suspect I am missing something very simple. 我怀疑我缺少一些非常简单的东西。

The reason I created var Calc was because I wanted to export the 'calculator' function. 我创建var Calc的原因是因为我想导出'calculator'函数。

the error: 错误:

/Users/alex/zdev/react-project/calc.js:4
    var add = function(){
    ^^^

SyntaxError: Unexpected identifier

file calc.js: (file has been shortened to stay concise) 文件calc.js :(为简化起见,文件已缩短)

var readline = require('readline-sync');

var Calc = {
        var add = function(){
                var num1 = readline.question("num1: ");
                var num2 = readline.question("num2: ");
                console.log(num1 + num2);
        };
}

module.export = Calc;

calling file: 调用文件:

var calc = require('./calc');

var Calc = new calc.Calc();

Calc.add();
Calc.sub();
Calc.divide();

You define a new object Calc with a function add , but the syntax is incorrect. 您使用函数add定义了一个新对象Calc ,但是语法不正确。 The correct syntax is: 正确的语法是:

var Calc = {
  add: function() {
    var num1 = readline.question("num1: ");
    var num2 = readline.question("num2: ");
    console.log(num1 + num2);
  }
};

If you want to make constructor function (I mean from your syntax) you should do it like this: 如果要使构造函数起作用(我的意思是从语法上来说),则应这样做:

function Calc() {
}

Calc.prototype.add = function() {
    var num1 = readline.question("num1: ");
    var num2 = readline.question("num2: ");
    console.log(num1 + num2);
};

module.exports = Calc;

and then you import this like: 然后像这样导入:

var Calc = require('./calc');

var calc = new Calc();

calc.add();
calc.sub();
calc.divide();

But I prefer for you to use ES6 class syntax, and the Calc constructor function will look like: 但我更希望您使用ES6类语法,并且Calc构造函数将如下所示:

class Calc {
    constructor() {}

    add() {
        var num1 = readline.question("num1: ");
        var num2 = readline.question("num2: ");
        console.log(num1 + num2);
    }
}

module.exports = Calc;

I suggest using JavaScript classes introduced in ECMAScript 2015 我建议使用ECMAScript 2015中引入的JavaScript类

class Calculator {
  constructor() {
    console.log("[Calc] created!");
  }

  static add(a, b) {
    return a+b;
  }

}

let Calc = new Calculator();

solution is as follows: 解决方法如下:

call file: 通话档案:

var calc = require('./calc');
var Calc = calc.Calc;
Calc.add();

calc file: Calc文件:

var Calc = {
        add: function(){
                var num1 = readline.question("num1: ");
                var num2 = readline.question("num2: ");
                console.log(num1 + num2);
        },
        divide: function(){
                var num1 = readline.question("num1: ");
                var num2 = readline.question("num2: ");
                console.log(num1 / num2);
        },
        sub: function(){
                var num1 = readline.question("num1: ");
                var num2 = readline.question("num2: ");
                console.log(num1 - num2);
        }
}

module.exports = {Calc:Calc}

the following lines pulled are where the original mistakes were: 以下几行是原始错误所在:

defining my class after importing from other function 从其他函数导入后定义我的班级

Calc = calc.Calc;

using a commas to seperate my object properties instead of a semicolon 使用逗号分隔对象属性而不是分号

},

not defining a dictionary in module exports. 没有在模块导出中定义字典。 Also, I wrote 'module.export' not 'module.exports' originally 另外,我最初写的是“ module.export”而不是“ module.exports”

module.exports = {Calc:Calc}

And I forgot to put my parseInt() for my num1 and num2 values. 而且我忘了将parseInt()用作num1和num2值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM