Answer the question
In order to leave comments, you need to log in
How to set variables in scope for a new instance of a class?
Greetings!
There is a class in the file A.js
:
class A {
do() {
console.log(some_var);
}
}
module.exports = A;
index.js
:const A = require('./A');
function print_it(param) {
let some_var = param;
let a = new A;
a.do();
}
print_it('wow'); // В консоли должно вывестись: wow
print_it('123'); // В консоли должно вывестись: 123
index.js
without changing in A.js
so that some_var becomes readable? Answer the question
In order to leave comments, you need to log in
do not invent a rake, but do it in a simple way:
// A.js
class A {
constructor(some_var) {
this.some_var = some_var;
}
do() {
console.log(this.some_var);
}
}
module.exports = A;
// index.js
const A = require('./A');
function print_it(param) {
const a = new A(param);
a.do();
}
print_it('wow'); // В консоли должно вывестись: wow
print_it('123'); // В консоли должно вывестись: 123
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question