简体   繁体   中英

How do I change variable in a function from another file?

I have some code like this:

//app.js
var r=require("./second.js");
var a=1;
r.second(a);
console.log(a);

And my second.js looks like this:

//second.js
module.exports.change=function(a){
    a=2;
}

Currently console.log(a) only show 1, I did a bit of research saying I can't pass it as a variable because variable is passed as value. I thought about using global.a. However, it doesn't work too because a is stored as a module variable, not a global variable, and I don't want to set it as a global variable.

How do I solve it?

The main answer is that you can't do it exactly as you asked. A variable defined as you have a in app.js is private to the scope of a and only other code within app.js can modify it.

There are a number of other ways you can structure things such that module B can modify something in module A or cause something to be modified in module A.

Global

You can make a global. This is not recommended for a variety of reasons. But you could make it a property of the global object by changing your declaration of a to:

global.a = 1;

Then, in module B, you can directly set:

global.a = 2;

Pass and Return

You can pass a into a function in module B and have that function return a new value that you assign back into a .

const b = require('moduleB');

let a = 1;
a = b.someFunction(a);
console.log(a);    // 2

Then, inside of moduleB, you can modify propertys on a directly:

// inside of moduleB
module.exports.someFunction = function(a) {
    return ++a;
};

Put value in object and pass reference to object

Rather than storing the value in a simple variable, you can store it as a property of an object an you can then pass a reference to that object. Objects in Javascript are passed by ptr so some other function you pass the object to can modify its properties directly.

const b = require('moduleB');

let a = {someProperty: 1};

b.someFunction(a);
console.log(a.someProperty);    // 2

Then, inside of moduleB, you can modify propertys on a directly:

// inside of moduleB
module.exports.someFunction = function(obj) {
    obj.someProperty = 2;
};

Initially, there isn't any function that is called second . Your module exposes a function called change . Then when you pass a value to function this value is copied and whatever change you do affects only the copy. So passing the value of a , the change affects only the copy of this value and not the original one. What could you do is to pass an object. This time a copy of the reference to the object is passed. So any change you make is reflected back to the original object (it's like pointers in C and C++).

In terms of code, the changes you should make are the following:

// You could make the method more dynamical passing the value you want a get.
module.exports.change = function(obj, value){
    obj.a = value;
}

Last you should call it as:

var r=require("./second.js");
var obj = { a: 1};
r.change(obj,2);
console.log(obj.a);

First way is return in your change function in second.js .

app.js:

var r=require("./second.js");
var a=1;
a = r.change(a);
console.log(a);

second.js:

module.exports.change=function(a){
    a=2;
    return a;
}

Second way is use object as parameter to pass it by reference:

app.js:

var r=require("./second.js");
var obj={a:1};
r.change(obj);
console.log(obj.a);

second.js:

module.exports.change=function(obj){
    obj.a=2;
}

You can use localStorage or sessionStorage for setting and getting your variable. for node js you can use https://www.npmjs.com/package/localStorage package

//You can set using localStorage.setItem('key','value')

var r = require("./second.js");
var a = 1;
localStorage.setItem('a', 1) /*Window api for setting variable locally 
localStorage.setItem('key','value')*/
r.second(a);
console.log(a);

You can get using localStorage.getItem('key')

//second.js
module.exports.change=function(a){
a=localStorage.getItem('a')
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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