Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify arguments in self executing function

I want to be able to modify the arguments passed to a self executing function.

Here is some sample code:

var test = 'start';
(function (t) {t = 'end'} )(test);
alert(test) //alerts 'test'

And here is a fiddle. The variable test has not changed. How can I alter it, as in pass-by-reference?

like image 789
tckmn Avatar asked May 08 '26 08:05

tckmn


1 Answers

Pass in an object, it is pass-by-reference:

var test = {
    message: 'start'
};
(function (t) {t.message = 'end'} )(test);
alert(test.message)

FYI, Array is also pass-by-reference.

like image 136
Amy Avatar answered May 09 '26 22:05

Amy



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!