function - Scope in javascript acting weird -
object passed reference in javascript. meaning change in object should reflected. in case, expected output {} console.log(a)
function change(a,b) { a.x = 'added'; = b;//assigning {} b } a={} b={} change(a,b); console.log(a); //expected {} output {x:'added'} console.log(b) what happening here? should not because of functional scope far know. thank you
if added line can clearer picture of happening:
function change(a,b) { a.x = 'added'; = b; a.x = 'added well'; }; a={}; b={}; change(a,b); console.log(a); //{x:'added'} console.log(b); //{x:'added well'} when you're doing a = b you're assigning local variable a reference b holding.
Comments
Post a Comment