开发者

If an object is passed as an argument in a function, does it create a new instance?

开发者 https://www.devze.com 2023-02-06 06:08 出处:网络
In this code: function myFunction(obj) { console.log(obj); } var obj = { key : \'value\', funct :function(){},

In this code:

function myFunction(obj) {
  console.log(obj);
}

var obj = {
  key : 'value',
  funct :  function(){},
  elem  :  document.getElementById('navigation').getElementsByTagName('a')
}
开发者_如何转开发
myFunction(obj);

Is the object "obj" printed in the console inside myFunction the same object i defined or is a new instance?

Also, what if i pass it from myFunction to another function?


That's easy enough to answer for yourself. Try this code and see what happens:

function Test(obj) {
  obj.Whatever = "!";
}

var testObj = { original: true };
Test(testObj);
alert(testObj.Whatever);

If the alert shows "undefined", then you made a copy when passing into the function. If the alert shows "!", then you passed the object itself (or reference, or pointer) into the function.

I'm pretty sure you'll see the "!"


It's the same object, not a new instance. What gets passed into the function is a reference to the object, not a copy of the object. JavaScript is a purely pass-by-value language. The values passed into functions (and held in variables, etc.) for objects are references to the object, not the object itself.

You can readily prove that the object is not copied by modifying it in the function and seeing if you see those mods:

function foo(obj) {
    alert(obj.prop1);
    obj.prop2 = "foo";
}
var thingy = {
    prop1: 'bar'
}
foo(thingy);
alert(thingy.prop2);

Live example

With the above, first you see the alert from foo showing the prop1 property of thingy, then you see the alert from the outer code showing the prop2 that foo assigned to it.


All non-primitives are passed by reference in JavaScript -- that is, they are the same object. However, primitives are passed by value instead:

function change(a) {
    a = 2;
}
var a = 1;
change(a);
alert(a); // should alert 1

function change2(a) {
    a.test = 2;
}
var obj = {test:1};
change2(obj);
alert(obj.test); // should alert 2


It should be the same object you defined, and it will continue to the same object no matter how many times you pass it. Why? Are you seeing something to indicate this is not the case?

You might be getting confused because primitive values are passed by value, while objects are passed by reference.

This is a pretty decent description of the difference : http://snook.ca/archives/javascript/javascript_pass

The punch line can is his code examples :

Here is a an example that handles a primitive value, an integer in this case. With primitive values, Primitive values are passed by value meaning a new copy of the value is place on stack with function call. It also means that any changes made to a value within a function are local to that function.

function myfunction(x) 
{
      // x is equal to 4
      x = 5;
      // x is now equal to 5
}

var x = 4;
alert(x); // x is equal to 4
myfunction(x); 
alert(x); // x is still equal to 4

This is an example that shows how pass by reference works. In this case a reference to an object is provided to the function and any changes to that object are seen by everyone who has the reference.

function myobject()
{
    this.value = 5;
}
var o = new myobject();
alert(o.value); // o.value = 5
function objectchanger(fnc)
{
    fnc.value = 6;
}
objectchanger(o);
alert(o.value); // o.value is now equal to 6

One final thing, if you replace the reference passed into a function with a new reference this change is not reflected outside the function. Building off the previous example:

function myobject()
{
    this.value = 5;
}
var o = new myobject();


function objectchanger2(fnc)
{
  function myObject2() 
     { 
        this.value = 10
     }
   fnc = new myObject2()
}

objectchanger(o);
alert(o.value); // o.value is still  5


I believe javascript passes by reference so myFunction will see a reference to the same object.

0

精彩评论

暂无评论...
验证码 换一张
取 消