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?

16
  • "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) Commented Feb 18, 2020 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:) Commented Feb 18, 2020 at 15:23
  • 1
    You mention pointers. AFAIK JavaScript doesn't doesn't really have them? wafy.me/tech/2016/07/02/call-by-sharing.html Commented Feb 18, 2020 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? Commented Feb 18, 2020 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? Commented Feb 18, 2020 at 15:30

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.