0

Within Javascript I am having the next funtion 'myFunction'.

Note: I created the following code just for explaining my question.

var arrayReference = [1, 2];

function myFunction() {
    var array = getArray();

    array.forEach(function (elem) {
        remove(elem.attr('id'));
    });
}

function getArray() {
    return arrayReference;
}

function remove(elem) {
     $.each(arrayReference, function (index, el) {
          if (el == element) {
               arrayReference.splice(index, 1);
          }
     });
}

I would think that var array is just passed by value which will not be affected by a change in arrayReference. Yet it seems to be passed by reference. When the forEach method is called using the fetched array, it executes the remove method just for the first element of the array.

So to be clear: the remove functionality is only executed once in my case. Can this have anything to do with passing by reference? I have no idea what can cause this otherwise.

Good to know: When I comment out this remove function, the foreach is executed twice.

Klyner
  • 3,983
  • 4
  • 26
  • 50
  • This has nothing to do with pass-by-reference. It is just shared mutable state. The solution is easy: either don't share state or don't mutate state. Or both. – Jörg W Mittag Mar 23 '18 at 08:43
  • 1
    @Stphane: That is not true. ECMAScript / JavaScript does not have pass-by-reference. Everything is always pass-by-value. I think this answer explains it fairly well: https://stackoverflow.com/a/41306426/2988 although the accepted and the top-voted answer are good as well. Then, there is this classic article: http://javadude.com/articles/passbyvalue.htm which is technically about Java, but because of their common Smalltalk heritage (Java via Objective-C, ECMAScript via Self) applies to both. – Jörg W Mittag Mar 23 '18 at 08:48
  • Jörg W Mittag: how am I sharing the mutable state? – Klyner Mar 23 '18 at 09:05
  • You have two variables (`arrayReference` in global scope and `array` in `myFunction`) both pointing to the same mutable array. – Jörg W Mittag Mar 23 '18 at 09:10
  • I would say that once `array` is defined, it is no longer attached to `arrayReference`. Is that not true? How should I else define this variable sothat it doesn't change by a change within `arrayReference`? – Klyner Mar 23 '18 at 09:51
  • This has *nothing to do with how you define the variables*. This isn't about *pass-by-reference*. You have one array. You change that array. Then, the array is changed, regardless of which name you use to refer to it. If you want to mutate one array and have another array that doesn't change, then you need *two* arrays, you need to create *two* arrays. Or, alternatively, you *don't* mutate the array, but instead create a new one. Then you can share the original one without problems. – Jörg W Mittag Mar 23 '18 at 10:08
  • Thanks for explaining. Should I, instead of directly passing the array into the variable `array` passing the values one by one into a newly created array? Is that what you mean? – Klyner Mar 23 '18 at 10:57
  • I don't know what "passing a value into a variable" means. You pass arguments into functions, but I have never heard the term "passing a value into a variable". I also don't know what you mean by "passing the values one by one into a newly created array". Honestly, I don't even understand what the problem is you are trying to solve. From what I understand, you are using 21 lines of code plus 10368 lines of JQuery to implement a replacement for `map` and `filter` from the ECMAScript standard library. Why do you want to replace one simple line of code with 10389 lines of code? – Jörg W Mittag Mar 23 '18 at 16:00

0 Answers0