javascript - When are variables evaluated when passing them into functions? -
this simple question, know answer.
are arguments passed function calculated once , set local variables or calculated every time they're used inside function?
for example:
when write forloop, should set variable finds object using iteration:
for(var = 0; < objects.length; i++) { var obj = objects[i]; obj.title = "title"; obj.description = "description"; }
if don't set obj
variable operation finding object run more once:
for(var = 0; < objects.length; i++) { objects[i].title = "title"; objects[i].description = "description"; }
so far i've learnt bad (although i'm guessing performance difference in modern browsers unnoticeable).
my question is, if wrapped modifying methods in function , passed objects[i]
function, objects[i]
calculated once , set local variable obj
in function or calculate every time obj
called?
what better practice, code or code b?
code a:
function modify(obj) { obj.title = "title"; obj.description = "description"; } (var = 0; < objects.length; i++) { modify(objects[i]); }
code b:
function modify(obj) { obj.title = "title"; obj.description = "description"; } (var = 0; < objects.length; i++) { var obj = objects[i]; modify(obj); }
update: question similar different this question because questions when value calculated rather value passed.
my question is, if wrapped modifying methods in function , passed objects[i] function, objects[i] calculated once , set local variable obj in function?
yes. once value passed, it's passed. doesn't recompile argument each time.
therefore, code "better", doubt makes of difference @ all....
Comments
Post a Comment