Phillip Senn
Phillip Senn

Reputation: 47595

Are objects passed by reference or value in JavaScript?

In this post, the author is using:

$.fn.wPaint = function(settings) {
   settings = $.extend({}, defaultSettings, settings || {});

Q: Wouldn't that overwrite the settings variable the calling scope? I would think you'd want to create a new variable, which would mean that you'd have a total of 3 variables:

  1. The Default settings in the closure scope
  2. The settings in the arguments
  3. The settings in the local scope

Upvotes: 1

Views: 168

Answers (5)

alextercete
alextercete

Reputation: 5151

You could pass the settings object as the first argument to the function $.extend:

$.fn.wPaint = function(settings) {
    $.extend(settings, defaultSettings, settings || {});
}

See this example http://jsfiddle.net/26Bst/ and read the API entry for more details.

Upvotes: 1

sissonb
sissonb

Reputation: 3780

If you want to do this you'll need to pass setting inside an object {}.

$.fn.wPaint = function(settingsContainer) {
    settingsContainer.settings = $.extend({}, defaultSettings, settings || {});

-

var settingsContainer = {settings:originalSettings};
$.fn.wPaint(settingsContainer);
originalSettings = settingsContainer.settings;

Upvotes: 1

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123423

In JavaScript, all arguments are passed "by value." The key is that, in the case of settings, the value is a reference.

This is not the same as "by reference." It can be similar as you are still referencing the same object in memory:

$.fn.wPaint = function(settings) {
    settings.foo = 'foo'; // updates object in calling scope
    // etc.
};

But, changing settings itself only changes its own value to a new reference:

$.fn.wPaint = function(settings) {
    settings = $.extend(...);
};

Upvotes: 1

ZER0
ZER0

Reputation: 25322

Objects are always passed by reference.

In your code, you're just say that the local variable settings will point to a different objects. So you pass the object by reference then you said "ok, I'm not interested in that reference anymore, point to that other object".

To be clear, if you have:

var a = {foo: "bar"};
var b = a;

b = {bar: "foo"};

You're not "destroying" neither "change" the original object {foo: "bar"}. You just point b to a different object, a will still point to the original one, so won't be affected.

It's the same in your scenario.

Upvotes: 4

mikerobi
mikerobi

Reputation: 20878

The settings argument is a reference to a variable in the calling scope. Inside your function you are assigning a new object to the settings variable, this breaks the reference and points the local variable to a new value in the local scope. If you modified settings, without assigning to it, it would affect the settings in the calling scope. The settings in the arguments is the settings in the local scope.

Upvotes: 2

Related Questions