4

I have a clear function

function clear(item) {
    item = null;
    item = {};
}

which I call as

var myItem = {d:"test"};
clear(myItem);

and when I check the value of myItem It's Object { d="test" } [ala Firebug].

Obviously I have a flawed understanding of javascript as I expected the value of myItem to be {}.

I tested

function setValue(item) {
    item = {c:"bam"}; 
}

and myItem is still Object { d="test" }.

I'm not understanding why. Baffled I tells ya... baffled.

I have two parts to the question component of this post: A) Why isn't myItem getting set to the new values? B) Can I 'clear' myItem via a function like I'm trying to do.

I thought I had a pretty good grasp on javascript and this comes along and blows my confidence out of the water... sigh

7 Answers 7

1

Well, myItem is not modified or cleared because you're not assigning anything to it.

You're actually assigning null, {} and {c: "bam"} to a function parameter named item, which is roughly the same as a local variable: it does not change the original object passed by the caller.

Sign up to request clarification or add additional context in comments.

Comments

1

No because when you pass the value you're not changing the original element. You would need to either grab the this element or you would need to return the value and assign it like me = clear(me)

Comments

0

Javascript does not pass references to variables.

Writing item = something changes the parameter to refer to a different value.
It doesn't affect whatever the caller passed in.

By contrast, writing item.property = something modifies the object to add a property.
Objects are passed by reference, so the caller's object is mutated.

2 Comments

Completely wrong Objects are passed as Reference and not value so updating the functions parameter will update the object. however in the author of this question is not referencing the object properly
@The: I said that: Objects are passed by reference. However, variables are not passed by reference; you cannot change the caller's variable to refer to a different object.
0

item = null reassigns item, a reference, to null. It doesn't modify the object previously referred to by item.

It may be possible to delete each property in object via reflection. You can certainly delete item.d to remove that particular property.

Edit: here's the idea

for(var property in object) {
   if(object.hasOwnProperty(property)) {
      delete object[property];
   }
}

Comments

0

It a whole "this" keyword thing and how functions are called and what gets referenced.
Some good reading


In the mean time use this function to update attributes.

function objectchanger(obj, att, val){
    obj[att] = val;
}


var myItem = {d:"test"};
alert(myItem.d)
objectchanger(myItem, 'd', 'asdf')
alert(myItem.d)

Comments

0

Lots of correct answers but I don't think any is very clear. Maybe this piece of documented code will help you understand

// This creates a reference to an object
var myObj = {a:1, b:2};

// This assigns a new reference to the same object
var ref = myObj;

// The following statement does not affect myObj at all
// And it's the exact same thing as when you pass an object
// to a function, you are creating a new reference to that object,
// not a reference to the variable.
ref = null;

// define clear that doesn't do what we want
function clear(obj) {
   obj = null;
}

// Calling the function, creates a new reference to the object that is passed
// to the function, just like our example above, you can't change what
// other variables are pointing to
clear(myObj);

Comments

0

Here's a long code example showing the differences between references to an object and the object itself:

// we define a new object
var person1 = {name:"harry"};

// we define a reference to that object
var person2 = person1;

// if we change a property of the reference, this change will be reflected in the original object
person2.name = "henry";

// Example:
document.write(person1.name); // henry, because person2 is a reference to person1 therefore changing person2 changes person1

// however, if we destroy the reference, we do not destroy the original object
person2 = null;

// Example:
document.write(person1.name); // henry because destroying the reference to person1 does not destroy person1

// when we pass an object to a function, a reference to that object is created in the function
function setName(person3) {
    // at this point person3 is a reference to person1, so changing person3.name would change person1.name, however…
    person3 = {name:"Jane"}; // here the reference is destroyed and a new object is created
}

setName(person1);

// Example:
document.write(person1.name); // henry, because the reference to person1 was destroyed and person3 was defined as an entirely new Object


// If we destroy the reference in a function, the original object remains
function kill1(person4){
    person4 = null; // this just kills the reference to person1
}

kill1(person1);

// Example:
document.write(person1 == undefined); // false, because we only killed a reference


// However, we can test if the reference in a function refers to an original object, and if so destroy the original object
function kill2(person5){
    switch (person5){
        case person1:
            person1 = null;
            break;
        // could have more cases for other objects here
    }
}

kill2(person1);

// Example:
document.write(person1 == undefined); // true, because we killed the real thing

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.