0

Trying to complete a simple node.js exercise, I have tried several variations on this. I suspect I am missing something very simple.

The reason I created var Calc was because I wanted to export the 'calculator' function.

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)

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();
3
  • Should Calc be a constructor, from which you could instantiate many object instances each with their own instance data (which is not something you show being needed)? Or should should Calc just be a singleton object with some methods and you don't have to instantiate one separately to use it?
    – jfriend00
    Aug 21, 2017 at 23:01
  • I believe the second use if what I desired. I posted my own answer to this question below. Which gives what I wanted, unfortunately I am having trouble describing that. Aug 22, 2017 at 0:39
  • Can someone suggest a different title to this question? It may seem basic, but if better labeled it could be useful to others. Aug 22, 2017 at 0:40

4 Answers 4

2

You define a new object Calc with a function add, but the syntax is incorrect. The correct syntax is:

var Calc = {
  add: function() {
    var num1 = readline.question("num1: ");
    var num2 = readline.question("num2: ");
    console.log(num1 + num2);
  }
};
4
  • The OP appears to want Calc to be a constructor and add to be a method on an instantiated object. This code makes Calc a singleton and add a method on that singleton which is not how the OP shows they want to use the code.
    – jfriend00
    Aug 21, 2017 at 22:59
  • 1
    @jfriend00 In the code of OP Calc is used as a singleton. So my answer is about how to declare an object correctly. Prototypes, etc, are out of the scope.
    – alexmac
    Aug 21, 2017 at 23:11
  • I know what your code is. It is unclear what the OP wants. Their code for using Calc shows it as a constructor which is not what this creates, but alas their code shows no use of instance data so maybe a singleton would work just fine if they changed the calling code. Who knows? I've ask the OP to clarify in a comment to their question.
    – jfriend00
    Aug 21, 2017 at 23:12
  • sorry for being so vague. alexmax and jfriend, your dialogue has helped clear things up for me. Aug 22, 2017 at 0:54
0

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:

class Calc {
    constructor() {}

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

module.exports = Calc;
0

I suggest using JavaScript classes introduced in ECMAScript 2015

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

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

}

let Calc = new Calculator();
0

solution is as follows:

call file:

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

calc file:

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.exports = {Calc:Calc}

And I forgot to put my parseInt() for my num1 and num2 values.

4
  • Why not just accept the answer from @alexmac. It already shows you this proper syntax for declaring an object literal with properties.
    – jfriend00
    Aug 22, 2017 at 0:44
  • Sure if you think that is the right thing to do. Unfortunately after looking at his answer i was still hung up on how to properly do the module.exports. Aug 22, 2017 at 0:49
  • 1
    Well, the complication is that you asked a question that left out a lot of detail. You showed an object definition that was invalid syntax and then showed a way of using that object which was inconsistent with what you were trying to do and you didn't explain that actual objective here. So, your answer then filled in all the blanks in the question which is the wrong place to do that. What you should have done is edited your question to describe what you're actually trying to accomplish (describe the end goal for how you want to use it) and then we could answer that question more completely.
    – jfriend00
    Aug 22, 2017 at 1:20
  • alright thank you for letting me know. I will keep this in mind next time I create a question. Aug 22, 2017 at 1:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.