0

in the other programming languages we use & keyword for passing variable by reference.

for example, in php;

$a = 10;

function something(&$a){
    $a = 7;
};

something($a);

echo $a;
// 7

How can we do this in javascript ?

When user click the right or left arrow, I'm trying to get next or prev. image by array index;

list: function (index) {
    let items = this.images;
    return {
        next: function () {
            if (index > items.length -1) {
                index = 0;
            }
            return items[index++];
        },
        prev: function () {
            if (index < 0) {
                index = items.length -1;
            }
            return items[index--];
        }
    }
}

Outside of this iterator, I need to use index variable. But I only get old value... I want to get current index.

Teoman Tıngır
  • 2,766
  • 2
  • 21
  • 41
  • Javascript really doesn't have the distinction of variables being passed by reference or not. Tell us what you are trying to accomplish to avoid us dealing with X-Y problems and we can try and help you better. – Matt Runion Sep 22 '18 at 18:31
  • 2
    In JavaScript, variables are scoped based on how and where they are defined. To understand how this works, read this: https://github.com/getify/You-Dont-Know-JS/tree/master/scope%20%26%20closures – wlh Sep 22 '18 at 18:33
  • 2
    Either you store the value on an object and mutate the object or you create your own construct for references. – MinusFour Sep 22 '18 at 18:33
  • I think this is your answer https://stackoverflow.com/a/1733647/10390598 – DerMolly Sep 22 '18 at 18:36
  • @mrunion I updated my question – Teoman Tıngır Sep 22 '18 at 18:39
  • @MinusFour I tried to store index in a object and it seems work as well – Teoman Tıngır Sep 22 '18 at 18:42
  • 1
    @MadaraUchiha damn it, I just realised that. It was in the related tab and I just assumed it's the other question. I meant this one: https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language?rq=1 – VLAZ Sep 22 '18 at 18:50

1 Answers1

5

JavaScript is always pass-by-value, there's no concept of pass-by-reference in JavaScript*.

You can mimic the effect by using a primitive version of an atom:

let indexAtom = {value: 0};

function changeIndex(atom) {
  atom.value = 5;
}

changeIndex(indexAtom);

assert(indexAtom.value === 5);

I will say that if you need this, you usually have a code smell, and need to rethink your approach.

In your case, you should use a closure to achieve the same effect:

list: function (startingIndex = 0) {
    let items = this.images;
    let index = startingIndex; // note that index is defined here, inside of the function
    return {
        next: function () {
            // index taken from closure.
            if (index > items.length -1) {
                index = 0;
            }
            return items[index++];
        },
        prev: function () {
            // same index as the next() function
            if (index < 0) {
                index = items.length -1;
            }
            return items[index--];
        }
    }
}

* A common misconception is that objects are pass-by-reference, it's confusing because an object's "value" is also referred to as its "reference", programmers and naming things. Objects are pass-by-value as well, but the value of an object is a special "thing" called its "reference" or its "identity". This allows multiple variables to hold the same "reference" to the same object.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • In C++ you can choose to pass objects by value (copy it) or by reference. In JavaScript there's no choice. And passing objects in JavaScript is similar to passing objects by reference in C++. – marzelin Sep 22 '18 at 18:58
  • @marzelin: "And passing objects in JavaScript is similar to passing objects by reference in C++." No. You can't "pass objects" in JavaScript because "objects" are not values in JavaScript. Like Java, the only values in JavaScript are primitives and references, where references are pointers to objects. Whenever you create an object, you get back a reference (pointer to object); when you access a field of an object, you do it through a reference that points to it. This is like pointers to objects in C++. – newacct Oct 25 '18 at 01:46
  • @newacct pointers and references are two different things. Values in Javascript have types and one of the possible types is Object. An object is basically a collection of properties. By passing an object I mean making an object accessible from a different stack. There is no reference data type in JavaScript. References are a language implementation detail that has one important consequence: changing object in one place changes it everywhere else. – marzelin Oct 25 '18 at 13:19
  • @marzelin: Nope. The only values in JavaScript are primitives, and pointers to objects (reference). Anything you can put in a variable, that you get as the result of an expression, or that you can pass, are either primitives or references (pointer to objects). Never "objects". It the exact same semantics as in Java. – newacct Oct 25 '18 at 16:37
  • @newacct I'm sorry but what you're saying is against ECMAScript specification. Valid value types in JavaScript are Undefined, Null, Boolean, String, Symbol, Number, and Object. A reference is one of Specification Types that are used to describe intermediate results or language semantics and are not directly manipulated by a JavaScript programmer. Moreover, ECMAScript implementation is not even required to use references under the hood. https://www.ecma-international.org/ecma-262/#sec-ecmascript-language-types – marzelin Oct 25 '18 at 21:42
  • @marzelin: It doens't matter what you call it. It matters what the semantics are. When you assign one variable holding a reference to an object to another variable, you have two variables with references to the same object, and modifications of the properties of the object through one reference can be seen through the other reference. And whatever object that the second variable used to point to is unaffected; it just can't be accessed through that second variable anymore. That shows that the value of a variable is not an object itself, but something that indirectly points to an object. – newacct Oct 26 '18 at 01:48
  • @marzelin: Some languages (e.g. Java) call that a reference. In C++, it is a pointer to an object. Some Python people call that a "name", but it is really hte same thing. They all describe the same semantics. Whatever you call it, it is not an object itself that is being assigned. Similarly, it is not an object that is being passed. – newacct Oct 26 '18 at 01:49