2

let say I've got this kind of code:

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

above code will pass false as argument to testCondition. How can I do to pass reference to obj1.test instead of passing it's value?

EDIT wow, thanks for quick responses!! :) But I would like to add, that I cannot pass the whole object, because I would like to build one generic function/method which would just check parameter and do onComplete callback or onError callback. Above code is only example of situation where I am right now.

asked Jul 17, 2013 at 13:31
5
  • Why do you want to cause a recursion stack overflow? Commented Jul 17, 2013 at 13:45
  • What would you need the reference for? Do you intend to assign to the object property? Commented Jul 17, 2013 at 13:46
  • I'm developing JS application which has many async AJAX requests, by above function I would like to test if requests are finished (many parallel requests, and all kind of crazy crap. ;) ) And because of D.R.Y. I dont want to have multifunctions to check same thing, but in different conditions. Commented Jul 17, 2013 at 13:52
  • Well in async environments they tell you when they're finished via callbacks - no need to test for it. Or maybe you should post your actual code; this abstracted version doesn't demonstrate your problem. Commented Jul 17, 2013 at 13:57
  • no, i think it would be pointless. Anyway I think I get the picture. thanks! Commented Jul 17, 2013 at 14:00

5 Answers 5

4

You have two choices, from what I can see:

  1. Pass the object itself, instead of its member. You can then access and modify the member:

    function testCondition(object) {
     if (!object.test) {
     testCondition(object);
     }
    }
    testCondition(obj1)
    
  2. Alternatively, since you're changing a single value, you can have that value be returned by the function:

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

FYI, your code as you've displayed it right now will cause an infinite recursion if condition is false.

hopper
13.5k7 gold badges52 silver badges54 bronze badges
answered Jul 17, 2013 at 13:38
Sign up to request clarification or add additional context in comments.

Comments

1

What's wrong with return values?

Alternatively you can wrap the argument in an object:

function foo(arg) {
 var val = arg.val;
 // do something with val
 arg.val = val;
}
var arg = {val:"bar"};
foo(arg);
// do something with arg.val
answered Jul 17, 2013 at 13:36

1 Comment

Indeed. Fixed it just now.
0

You can't.

Pass obj1 instead, then examine condition.test inside the function.

answered Jul 17, 2013 at 13:36

Comments

0

You can't. JavaScript passes objects and arrays by reference, primitives (integers, strings, booleans) by value. What you're asking for is impossible, except by bad work-arounds:

function ugly(result) {
 result.success = true;
}
var result = {};
ugly(result);

Instead, just return your value. It's how JavaScript is meant to work.

answered Jul 17, 2013 at 13:37

Comments

0

pass the whole object instead of its property:

testCondition(obj1);

and then

if(!passedObj.test){

etc...

answered Jul 17, 2013 at 13:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.