Nodejs closing variable not updated in module

I need help understanding NodeJ. I obviously missed something fundamental. I have a module similar to the following using the base template of the expansion module ...

var someArray = [];

var publicApi = {
    someArray: someArray,
    someFunction: someFunction
};

function someFunction() {
    someArray = ['blue', 'green'];
}

module.exports = publicApi;

When I use this module, someArray does not change when I call someFunction ...

var myModule = require('myModule');

myModule.someFunction();
var test = myModule.someArray;
// test is always an empty array

Please help me understand why. I understand that I can probably get this to work using the constructor, but I want to fill in the gap in my knowledge about why the above does not work.

Update:

I can get this to work with the following module modifications ...

var theModule = {
    someArray: [],
    someFunction: someFunction
};

function someFunction() {
    theModule.someArray = ['blue', 'green'];
}

module.exports = theModule;

, . , , , , ​​ , , .

+4
4

, , , , , , JavaScript Node:

var someArray = [];

var object = {
    someArray: someArray,
}
someArray = [1, 2, 3];
console.log(object.someArray);

[], object.someArray . :

var someArray = [];

, someArray. array1.

var object = {
    someArray: someArray
}

someArray, , someArray. , , array1, someArray. :

someArray = [1, 2, 3];

( array2), someArray. array1, someArray array2, .

, Node - someArray, publicApi.someArray publicApi.someArray, , .

, :

someArray -> array1[]
object.someArray -> array1[]

:

someArray -> array2[1, 2, 3]
object.someArray -> array1[]

, object.someArray .

+2

, , . , . "" . :

function SomeFunction(x) {
   var closureVar = x;

   toReturn = function(y) {
     var answer = closureVar;
     closureVar = y;
     return answer;
   }

   return toReturn;
}

var runIt = SomeFunction(15);

, SomeFunction, "closVar". , () . 'closVar', . , , "toReturn", .

. SomeFunction(), () closVar, toReturn, SomeFunction(). SomeFunction() .

0

FWIW, , :

function someFunction() {
    //someArray = ['blue', 'green'];
    someArray[0] = 'blue'
    someArray[1] = 'green'
}

, , [], - . , .

0

, get/set:

var someArray = [];

var publicApi = {
    getSomeArray: function ()  { return someArray; },
    setSomeArray: function (s) { someArray = s; },
    /*
     * Or if you know you can support get/set syntax:
     *
     * get someArray ()  { return someArray; }
     * set someArray (s) { someArray = s; }
     */
    someFunction: someFunction
};

function someFunction() {
    someArray = ['blue', 'green'];
}

module.exports = publicApi;

Get set MDN.

, , :

theModule.someArray = ['blue', 'green'];

But the problem that you see is due to the fact that you are replacing with a new array and not changing the array, for example.

function someFunction() {
    someArray.splice(0);
    someArray.push('blue', 'green');
}

I believe that the assignment causes the creation of a new reference to the object, and the modification of the existing object supports the existing link.

This is the result of a call exchange:
Is JavaScript a password or a language link? https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing

0
source

Source: https://habr.com/ru/post/1616575/


All Articles