Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript passing an object into function [duplicate]

var o = {};
(function(x){x=null})(o);       //o is NOT null after this statement
(function(x){x.foo = "foo"; x.bar = "bar";})(o)   //o has properties foo and bar after this statement

what is going on when passing object o into the function? the first function makes it seem o did not get passed; the second function makes it seem that o get passed

like image 521
delta2006 Avatar asked Sep 02 '26 08:09

delta2006


1 Answers

Line 1: An object is created. A reference to it is passed to o.

Line 2: A function is called. A reference to the object is passed as an argument. The reference (in x) is then overwritten with null (without touching the object itself or the reference to it still assigned to o).

Line 3: A function is called. A reference to the object is passed as an argument. A foo and bar property are added to the object and assigned values.

like image 64
Quentin Avatar answered Sep 03 '26 20:09

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!