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.
-
Why do you want to cause a recursion stack overflow?Bergi– Bergi2013年07月17日 13:45:41 +00:00Commented Jul 17, 2013 at 13:45
-
What would you need the reference for? Do you intend to assign to the object property?Bergi– Bergi2013年07月17日 13:46:52 +00:00Commented 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.Krystian– Krystian2013年07月17日 13:52:23 +00:00Commented 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.Bergi– Bergi2013年07月17日 13:57:30 +00:00Commented Jul 17, 2013 at 13:57
-
no, i think it would be pointless. Anyway I think I get the picture. thanks!Krystian– Krystian2013年07月17日 14:00:08 +00:00Commented Jul 17, 2013 at 14:00
5 Answers 5
You have two choices, from what I can see:
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)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.
Comments
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
1 Comment
You can't.
Pass obj1 instead, then examine condition.test inside the function.
Comments
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.
Comments
pass the whole object instead of its property:
testCondition(obj1);
and then
if(!passedObj.test){
etc...