javascript - JS - passing by reference an object -
given code below create object called foo , want make 'a' equal true function called maketrue(obj).
var foo = {a: false, b: false, c: false} function maketrue(obj) { obj = true; } maketrue(foo.a); // want make 'a' true function console.log(foo.a);
why return false still?
i have looked @ similar questions worked passing object method doesn't pass reference.
you're not passing object argument (that passed reference), you're passing in boolean value of object's property (pass value).
if want work:
var foo = {a: false, b: false, c: false} function maketrue(obj, val) { obj[val] = true; } maketrue(foo, 'a'); // want make 'a' true function console.log(foo.a);
Comments
Post a Comment