-1

is there a way to pass a variable itself, not a value in javascript? I remember being able to do so in flas as3 if i remember correct which was based on javascript. I'm not sure why i can't do the same here. Your help would be much appreciated.

variable1: false,

function1() {

    this.variable1 = true //this works of course console.log(this.variable1) prints true
}

function2() {

    var temparray1 = [this.variable1]
    temparray1[0] = true //does not work like i want, it's the value in the array that change, not this.variable1

    console.log(this.variable1) //prints still false
    console.log(temparray1[0]) //prints true
}
The Architect
  • 313
  • 2
  • 10
  • You can't do that. – SLaks Aug 02 '17 at 16:52
  • This is not how the language is supposed to work, and this would be extremely confusing to any JavaScript developer who has to use your code later. – sbking Aug 02 '17 at 16:56
  • 2
    Possible duplicate of [Javascript by reference vs. by value](https://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value) – Nisarg Shah Aug 02 '17 at 16:57
  • https://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value – kyle Aug 02 '17 at 17:01
  • Ok so how to achieve this dynamically. Let's say you have a table and for when you match an item in a array, you want to change the variable you chose at that index. – The Architect Aug 02 '17 at 23:48

3 Answers3

2

Primitive datatypes are always passed as value, never as a reference. Javascript passes objects as references though, so you can create an object and assign the value to an attribute like so:

variable1 = {yourValue : false}
...
var temparray1 = [this.variable1]
temparray1[0].yourValue = true;

Now when accessing variable1.yourValue it should be true.

ggradnig
  • 13,119
  • 2
  • 37
  • 61
0

There is no way to pass a boolean by reference in Javascript, but as a workaround you can wrap your boolean in an object, like this:

var variable1 = { value: false }

function setVar() {
    variable1.value = true
}

function test() {
    var temparray1 = [variable1]
    temparray1[0].value = true
    console.log(variable1.value) // prints true
    console.log(temparray1[0].value) // also prints true
}
Ned Howley
  • 828
  • 11
  • 19
0

Javascript always passes by value. So in your case

var temparray1 = [this.variable1]

becomes

var temparray1 = [false]

So changing it does not change variable1. But if you want to change variable1 by changing the array, you should either have variable1 as an array or object. For Example:

this.variable1 = {
    value: false
}

var temparray1 = [this.variable1];
temparray1[0].value = true;

Here also, Javascript passes by value, but now this.variable1 is a reference to the object and temparray1[0] has the value of variable1, So it is also a reference to the same object.So we are changing that object.

Boopesh
  • 149
  • 1
  • 2
  • 7
  • Thank you for your input. Also how can i fix this post so as to unlock my account, i cannot post and i don't know what is wrong. Or how to improve this question. Thanks. – The Architect Jul 28 '18 at 05:43