Javascript对象未被清除/重置功能

时间:2011-04-15 22:26:15

标签: javascript

我有一个明确的功能

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

我称之为

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

当我检查myItemObject { d="test" } [ala Firebug]的值时。

显然我对javascript的理解有误,因为我预计myItem的值为{}

我测试了

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

myItem仍为Object { d="test" }

我不明白为什么。我困惑地告诉你......困惑。

我对这篇文章的问题部分有两个部分: A)为什么myItem没有设置为新值? B)我可以通过像我正在尝试的功能“清除”myItem

我以为自己对javascript有了很好的把握,而且这种情况随之而来,让我对水的信心大增...... 叹息

7 个答案:

答案 0 :(得分:1)

否,因为当您传递该值时,您不会更改原始元素。您需要抓取this元素,或者需要返回值并将其指定为me = clear(me)

答案 1 :(得分:1)

好吧,myItem未被修改或清除,因为您没有为其分配任何内容。

您实际上是将null{}{c: "bam"}分配给名为item函数参数,这与a大致相同局部变量:它不会更改调用者传递的原始对象。

答案 2 :(得分:0)

Javascript不会传递对变量的引用。

item = something会更改参数以引用不同的值 它不会影响调用者传入的任何内容。

相比之下,写item.property = something会修改对象以添加属性 Objects 通过引用传递,因此调用者的对象发生了变异。

答案 3 :(得分:0)

item = null将引用item重新指定为null。它不会修改item之前引用的对象。

可以通过反射删除object中的每个属性。您当然可以delete item.d删除该特定属性。

编辑:这是想法

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

答案 4 :(得分:0)

这是一个长代码示例,显示了对象引用与对象本身之间的差异:

// 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

答案 5 :(得分:0)

它是一个完整的“this”关键词,以及如何调用函数以及引用的内容 Some good reading


同时使用此函数更新属性。

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


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

答案 6 :(得分:0)

很多正确的答案,但我认为不是很清楚。也许这段文档化的代码可以帮助您理解

// 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);