admin管理员组文章数量:1355081
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?
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?
5 Answers
Reset to default 6Unlike 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 piler 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.
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);
To print myString varible use console.log(this.myString);
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();
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;
本文标签: Difference between classes in Java vs JavaScriptStack Overflow
版权声明:本文标题:Difference between classes in Java vs JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743962533a2569293.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论