简体   繁体   English

如何从另一个文件更改函数中的变量?

[英]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看起来像这样:

//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. 当前console.log(a)仅显示1,我做了一些研究说我不能将其作为变量传递,因为变量作为值传递。 I thought about using global.a. 我考虑过使用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. 但是,它也不起作用,因为a存储为模块变量而不是全局变量,并且我不想将其设置为全局变量。

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. 变量定义为你有a在app.js是私有的范围a app.js中,只有其他代码可以修改它。

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. 您可以通过多种其他方式来构造事物,以便模块B可以修改模块A中的某些内容或使某些内容在模块A中被修改。

Global 全球

You can make a global. 你可以成为a全球。 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: 但是您可以通过将a的声明更改为a来使其成为全局对象的属性:

global.a = 1;

Then, in module B, you can directly set: 然后,在模块B中,您可以直接设置:

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 . 你可以通过a进入模块B的功能,并具有功能返回分配回一个新值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: 然后,moduleB里面,你可以修改propertys a直接:

// 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. Javascript中的对象由ptr传递,因此传递给该对象的其他一些函数可以直接修改其属性。

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: 然后,moduleB里面,你可以修改propertys a直接:

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

Initially, there isn't any function that is called second . 最初,没有任何函数称为second Your module exposes a function called change . 您的模块公开了一个名为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. 因此,通过价值a ,变化只影响该值的拷贝,而不是原来的一个。 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++). 因此,您所做的任何更改都会反映回原始对象(就像C和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 . 第一种方法是在second.js返回您的change函数。

app.js: app.js:

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

second.js: second.js:

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

Second way is use object as parameter to pass it by reference: 第二种方法是使用object作为参数通过引用传递它:

app.js: app.js:

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

second.js: second.js:

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

You can use localStorage or sessionStorage for setting and getting your variable. 您可以使用localStorage或sessionStorage来设置和获取变量。 for node js you can use https://www.npmjs.com/package/localStorage package 对于节点js,您可以使用https://www.npmjs.com/package/localStorage

//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') 您可以使用localStorage.getItem('key')

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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