0

Consider the following script:

let object = {
    text1: 'this is text1',
    text2: 'this is text2'
}

class Translate {
   constructor() {
     this.localization = null;
     this.lateForTheShow = [];

     init();
   }

   async init() {
      /** Perform an ajax call to fetch a localization file **/
      this.localization = await ajaxCall(...);

      for (let i in this.lateForTheShow) {
         this.translate(this.lateForTheShow[i].originalText, this.lateForTheShow[i].referencedVariable);
      }
   }

   translate(text, &pointer) {
      if (this.localization === null) {
        this.lateForTheShow.push({
             referencedVariable: pointer,
             originalText: text
        });
     } else {
       text = this.localization[text];
     }

     pointer = text;
  }
}

let TranslateObj = new Translate();
TranslateObj.translate(object.text1, object.text1);

The above code is not a valid javascript code, because you can't pass pointers to variables in javascript (at least certainly not the PHP way I did pass them). Something like this could be done in PHP although, and I am wondering if something simmilar could be achiavable in javascript, or not?

Adam Baranyai
  • 3,635
  • 3
  • 29
  • 68
  • _"A common but incorrect explanation is that the semantics in JavaScript for passing numbers and arrays are different. Some claim that numbers are passed by value, whereas arrays are passed by reference. Rather, it is more accurate to say that both arrays and numbers [and objects] are passed by sharing. Whereas arrays are mutable, numbers are not."_ - Does this quote help? ([] - added by me) – evolutionxbox Feb 18 '20 at 15:22
  • @evolutionxbox not really, no:P maybe I am tired after 8 hours of work, but it didn't get me closer to where I want to be:) – Adam Baranyai Feb 18 '20 at 15:23
  • 1
    You mention pointers. AFAIK JavaScript doesn't doesn't really have them? https://wafy.me/tech/2016/07/02/call-by-sharing.html – evolutionxbox Feb 18 '20 at 15:25
  • If you set `this.lateForTheShow` to an empty array and then call `init` where you have a loop on it, how do you expect that loop body to ever execute? – trincot Feb 18 '20 at 15:27
  • @trincot as the `init` function is an `async` function, `this.lateForTheShow` may very well get populated, by the time the code reaches that `for loop`. Or am I mistaken here? – Adam Baranyai Feb 18 '20 at 15:30
  • Can you be explicit about what `this.lateForTheShow[i].referencedVariable` is? Is it a primitive value or not? – trincot Feb 18 '20 at 15:30
  • @AdamBaranyai, Only code that would execute *after* an `await` would be asynchronously executed, and in this case there is just an awaited fetch. I don't see how a fetch would populate that array. – trincot Feb 18 '20 at 15:30
  • @trincot `this.localization = await ajaxCall(...);` it IS after an await... am I too tired to realize something here?:-? – Adam Baranyai Feb 18 '20 at 15:31
  • How do you see that `fetch` populate the array? You should at least show the code that populates it. There is no such thing in their question. – trincot Feb 18 '20 at 15:32
  • I omited the `ajaxCall` function, which should return a `Promise` that resolves to an `object` – Adam Baranyai Feb 18 '20 at 15:32
  • @trincot why do you think that the asynchronous function `ajaxCall`, which I ommited, is relevant to my question? – Adam Baranyai Feb 18 '20 at 15:33
  • No, no, I don't mean that, but it is relevant to know whether you populate the array, and with *what*. An ajax call does not populate an array -- it fetches a response. Maybe you can just avoid this discussion and provide code with which we can reproduce your issue. – trincot Feb 18 '20 at 15:34
  • @trincot I think that is totally irrelevant to my question, because I am not asking in help solving a bug on how I populate my array, I am asking about if something simmilar to pointers can be implemented for javascript primitives, like `string`. To answer one of your previous questions, as you can well see in my example also, the `this.lateForTheShow[i].referencedVariable` based on the above example, is a javascript primitive `string`, yet I am wondering, if somehow, I could make it a `pointer` to that same primitive – Adam Baranyai Feb 18 '20 at 15:36
  • As you now say it is a primitive. The answer is "no". – trincot Feb 18 '20 at 15:37
  • @trincot yes, I found that out based on the comments the previous posters did already, but my question still stands, as in: is it possible to write a code that would yield simmilar outcomes to the code above if run in an environment, where you can pass pointers? – Adam Baranyai Feb 18 '20 at 15:38
  • There are suggestions in the referenced Q&A. – trincot Feb 18 '20 at 15:39

2 Answers2

2

In JavaScript, objects are passed by reference by default. And only some types like strings, numbers, etc., are passed by value.

Example: pass by value

function myFunction(arg0 /*arg0 is the copied variable */) {
  arg0 += 10;
  console.log(aVar); //displays 15
} //goodbye arg0…
 
var aVar = 5;
console.log(aVar); //displays 5
myFunction(aVar);
console.log(aVar); //still displays 5

Example: pass by reference

function myFunction(arg0) {
  arg0.val = "I'm still a string!";
  console.log(arg0.val); //displays "I'm still a string!"
}
 
var aVar = {val: "I'm a string!"};
console.log(aVar.val); //displays "I'm a string!"
myFunction(aVar.val);
console.log(aVar.val); //displays "I'm still a string!"

A short answer to your question is that there's no way to pass a pointer, that I know of, but you can achieve similar behaviour by playing with the objects. JavaScript can be a messed up language in some cases.

References:

The code examples were taken from https://www.htmlgoodies.com/html5/javascript/passing-javascript-function-arguments-by-reference.html

There's also a better explanation of how it works in JavaScript

Pawel Veselov
  • 3,996
  • 7
  • 44
  • 62
Tim Nimets
  • 348
  • 2
  • 9
0

Objects are passed by reference in JavaScript, so make it an object and you have a pointer.

Josh Wulf
  • 4,727
  • 2
  • 20
  • 34