2

I have some experience with C# and Java, but I'm trying to learn javascript/node.js. I can't quite figure out what the problem is with this code.

So I have my main.js file, and it has this code:

const MyClass = require("./MyClass");
let myclass = new MyClass("my string!");

myclass.repeatString();

The MyClass it's calling has this code:

class MyClass {
    constructor(myString) {
        this.myString = myString;
    }

    repeatString() {
        console.log(myString);
    }
}

module.exports = MyClass;

When I try to run this, I get ReferenceError: myString is not defined when it tries to execute that repeatString() method. What am I doing wrong/what's the right way to do this?

nem035
  • 31,501
  • 5
  • 73
  • 88
Andrio
  • 1,243
  • 16
  • 35

5 Answers5

5

Unlike some languages like Java where we can reference variables within a class without using this, this binding behaves much differently in JavaScript, as does scoping.

In Java, this is legal:

class Test {
  int i;
  void method() {
    System.out.print(i); // <-- Java compiler knows to look for the `i` on the class instance (technically it first looks for `i` locally in the scope of `method` and the goes to look on the class)
  }
}

In JavaScript, we don't have this behavior, there's no such thing as a class variable (yet).

class Test {
  i; // <-- Uncaught SyntaxError: Unexpected token ;
  method() {
    console.log(i);
  }
}

For your example, this means you are always required to use this when referencing variables present in a class (technically on an object instance).

Thus you must use this.myString:

repeatString() {
  console.log(this.myString);
}

You are getting the Reference Error because the engine tries to find a variable called myString starting from the scope of repeatString and upward and there aren't any. The only scope that has a variable myString is the one within the constructor function, and that scope is unreachable from repeatString.

Classes in JavaScript are radically different (here's an overview) than Java, even though the syntax looks very similar.

nem035
  • 31,501
  • 5
  • 73
  • 88
1

To indicate that you're referring to a property of the object named myString instead of a variable with the same name, your console.log() invocation should read:

console.log(this.myString);
Robby Cornelissen
  • 72,308
  • 17
  • 104
  • 121
1

To print myString varible use console.log(this.myString);

Naveen Kerati
  • 795
  • 1
  • 9
  • 23
1

To access the variable of a class, use this. variable name.

Please check the code below.

class MyClass {
    constructor(myString) {
        this.myString = myString;
    }

    repeatString() {
        console.log(this.myString);
    }
}

const myClass = new MyClass('Hello');
myClass.repeatString();
Hassan Imam
  • 16,414
  • 3
  • 29
  • 41
0

Class member-variable in JavaScript can be accessed using this.member-variable syntax.

The correct way to solve the problem is following:

Main.js

const MyClass = require("./MyClass");
let myclass = new MyClass("my string!");
myclass.repeatString();

MyClass.js

class MyClass {
    constructor(myString) {
        this.myString = myString;
    }

    repeatString() {
        console.log(this.myString);
    }
}

module.exports = MyClass;
Abhay Kumar
  • 432
  • 2
  • 9