How to change Bool value within a function in javascript?

szuniverse

Based on this tutorial: link

Here is my example code:

function modifyVar(obj, val) {
  obj.valueOf = obj.toSource = obj.toString = function(){ return val; };
}

function setToFalse(boolVar) {
  modifyVar(boolVar, 'false');
}

var isOpen = true;
setToFalse(isOpen);
console.log('isOpen ' + isOpen); 

How can I change the bool variable value within a function? Is it possible to pass the bool value by reference? thanks in advance

T.J. Crowder

Several problems there:

  1. 'false' is not false.

  2. Variables are passed by value in JavaScript. Always. So there is no connection whatsoever between the boolVar in setToFalse and the isOpen you're passing into it. setToFalse(isOpen) is processed like this:

    • The value of isOpen is determined

    • That value (completely disconnected from isOpen) is passed into setToFalse

  3. JavaScript has some interesting handling around primitive types: If you try to use them like object types (var a = 42; a.toFixed(2); for instance), the value gets promoted to a temporary object, that object is used, and then the object is discarded. So if obj is false, obj.anything = "whatever" ends up being a no-op, because the object that temporarily exists ends up getting released as soon as the line finishes.

You could do something like what you're doing by promoting isOpen to an object via new Boolean, but beware that it will then act like an object, not a boolean:

function modifyVar(obj, val) {
  obj.valueOf = obj.toSource = obj.toString = function(){ return val; };
}

function setToFalse(boolVar) {
  modifyVar(boolVar, false);
}

var isOpen = new Boolean(true); // An object
setToFalse(isOpen);
snippet.log('isOpen ' + isOpen);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

That works because the value in isOpen is a reference to an object. So when that value is passed into setToFalse as boolVar, boolVar's value is a copy of that reference, and so refers to the same object. So that sorts out issue #2 above. Issue #3 is solved by creating an object explicitly, rather than relying on the implicit behavior.

But, remember my warning above about how it will act like an object (because it is one), not like a boolean? Here's an example:

function modifyVar(obj, val) {
  obj.valueOf = obj.toSource = obj.toString = function(){ return val; };
}

function setToFalse(boolVar) {
  modifyVar(boolVar, false);
}

var isOpen = new Boolean(true); // An object
setToFalse(isOpen);
snippet.log('isOpen ' + isOpen);
if (isOpen) {
  snippet.log("isOpen is truthy, what the...?!");
} else {
  snippet.log("isOpen is falsey");
}
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

We see:

isOpen false
isOpen is truthy, what the...?!

...because isOpen contains a non-null object reference, and non-null object references are always truthy.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to change Bool value within a function in javascript?

From Java

how to change the value of a random value within a javascript array?

From Dev

How do I globally change a variable value within function in lisp

From Dev

How to change the value of variable in a function in javascript?

From Dev

Can't change the value of an id within on function

From Dev

JavaScript - change value of button when text is selected within a DIV (and combine it in one simple function)

From Dev

How to call a function within another function in javascript

From Dev

How to call a function within another function in javascript

From Dev

How to return value from a callback within a function?

From Dev

How to retrieve a specific value within a map function?

From Dev

How to access a key value within State in a function?

From Dev

How to change value inside function

From Dev

Change value of member variable within function - C++

From Dev

Is it possible change value of Member variable within "const" function?

From Dev

How to call a JavaScript function from within Selenium?

From Dev

How to replace the value of a string within a string in javascript

From Dev

JavaScript Function within Function

From Dev

How To Change Value on selection in javascript

From Dev

Tuple bool value does not change

From Dev

How to change the default value of a function with another function?

From Dev

Easing Function in JavaScript - set change in value to 0

From Dev

How to pass a bool to a JavaScript function from code behind?

From Dev

How to change onsubmit function to onchange function on javascript

From Dev

Javascript prompt within a function not returning the value I expect

From Dev

javascript - pass a value from parent to child window and within a function

From Dev

updating attribute value of a object from within a setTimeout function in Javascript

From Dev

How to access the property function within a function object javascript

From Dev

Python- How to change the file name of a variable within a function

From Dev

How to call global function on route change within Durandal?

Related Related

  1. 1

    How to change Bool value within a function in javascript?

  2. 2

    how to change the value of a random value within a javascript array?

  3. 3

    How do I globally change a variable value within function in lisp

  4. 4

    How to change the value of variable in a function in javascript?

  5. 5

    Can't change the value of an id within on function

  6. 6

    JavaScript - change value of button when text is selected within a DIV (and combine it in one simple function)

  7. 7

    How to call a function within another function in javascript

  8. 8

    How to call a function within another function in javascript

  9. 9

    How to return value from a callback within a function?

  10. 10

    How to retrieve a specific value within a map function?

  11. 11

    How to access a key value within State in a function?

  12. 12

    How to change value inside function

  13. 13

    Change value of member variable within function - C++

  14. 14

    Is it possible change value of Member variable within "const" function?

  15. 15

    How to call a JavaScript function from within Selenium?

  16. 16

    How to replace the value of a string within a string in javascript

  17. 17

    JavaScript Function within Function

  18. 18

    How To Change Value on selection in javascript

  19. 19

    Tuple bool value does not change

  20. 20

    How to change the default value of a function with another function?

  21. 21

    Easing Function in JavaScript - set change in value to 0

  22. 22

    How to pass a bool to a JavaScript function from code behind?

  23. 23

    How to change onsubmit function to onchange function on javascript

  24. 24

    Javascript prompt within a function not returning the value I expect

  25. 25

    javascript - pass a value from parent to child window and within a function

  26. 26

    updating attribute value of a object from within a setTimeout function in Javascript

  27. 27

    How to access the property function within a function object javascript

  28. 28

    Python- How to change the file name of a variable within a function

  29. 29

    How to call global function on route change within Durandal?

HotTag

Archive