JavaScript passing argument via reference -


let i've got kind of code:

var obj1 = {test: false};  function testcondition(condition){   if (!condition){     testcondition(condition);   } }  testcondition(obj1.test); 

above code pass false argument testcondition. how can pass reference obj1.test instead of passing it's value?

edit wow, quick responses!! :) add, cannot pass whole object, because build 1 generic function/method check parameter , oncomplete callback or onerror callback. above code example of situation right now.

you have 2 choices, can see:

  1. pass object itself, instead of member. can access , modify member:

    function testcondition(object) {     if (!object.test) {         testcondition(object);     } } testcondition(obj1) 
  2. alternatively, since you're changing single value, can have value returned function:

    function testcondition(condition) {     if (!condition){         return testcondition(condition);     } } obj1.test = testcondition(obj1.test); 

fyi, code you've displayed right cause infinite recursion if condition false.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -