我有一个明确的功能
function clear(item) {
item = null;
item = {};
}
我称之为
var myItem = {d:"test"};
clear(myItem);
当我检查myItem
它Object { d="test" }
[ala Firebug]的值时。
显然我对javascript的理解有误,因为我预计myItem
的值为{}
。
我测试了
function setValue(item) {
item = {c:"bam"};
}
且myItem
仍为Object { d="test" }
。
我不明白为什么。我困惑地告诉你......困惑。
我对这篇文章的问题部分有两个部分:
A)为什么myItem
没有设置为新值?
B)我可以通过像我正在尝试的功能“清除”myItem
。
我以为自己对javascript有了很好的把握,而且这种情况随之而来,让我对水的信心大增...... 叹息
答案 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);