在JS对象中设置值 [英] Setting values in JS objects
问题描述
我正在尝试更改对象中的值以供日后使用。为什么不使用这种方法保留值?
我有以下代码:
< span class =code-sdkkeyword> window .myobj = {
a:{_ a:' _a here',_ b:' _ b here',_ c:' '},
b:{_ a:' _ a here',_ b:' _ b here',_ c:' '},
c:{_ a:' _ a here',_ b:' _ b here',_ c:' '}
// 等等。
}
myobj [' a'] ._ c = ' test';
alert(myobj [' a'] ._ c); // not test!
这适用于预定义的值,但不适用动态设置。
编辑:
我把它缩小到循环问题 - 键总是数组中的最后一个。
window .keys = Object.keys(服务器) );
for ( var i = 0 ; i< keys.length; i ++)
{
var key = keys [i];
var xhr = new XMLHttpRequest();
xhr.addEventListener(' load', function ()
{
alert(key); // 这是因为循环结束后发生这种情况
myobj [key] ._ c = this .responseText;
});
xhr.open(' GET',' test.php?s =' + myobj [key] ._ b);
xhr.send();
}
我的尝试:
直接设置的值,使用[ '_c']和设置MyObj中[ '一']到一个新的对象完全。
问题关键始终是数组中的最后一个确实正在发生,因为'load'事件只在循环结束后被触发,所以key
确实等于您拨打alert(键)
时的最后一个键。这个问题可以通过由该替换您的addEventListener
呼叫解决:
<预郎= JavaScript的> xhr.addEventListener(<跨度类= code-string> load',( function (k){
return function (){
alert(k);
myobj [k] ._ c = this .responseText;
};
})(key));
这看起来有点复杂,所以让我们把这段代码分解成不同的部分。
( function (k){...})(key)
以上是一个自动执行的函数。当函数自己执行时,我们传递
key
作为k
参数:k
将是addEventListener
被调用时的键,它将不会受到键更改的影响
变量。
在自执行函数中,我们返回一个新函数。我们需要这样做,因为我们不想立即运行alert
(和另一行):我们想要一个我们可以传递给addEventListener
以便它在'load'事件上运行。所以我们必须返回我们想要在'load'事件上触发的函数:
return function (){
alert(k);
myobj [k] ._ c = this .responseText;
};
我们在这里使用
k
而不是key
,因为我们想要使用自执行函数中不受键
影响的变量。
I'm trying to change the value in an object for use later. How come values aren't retained using this method?
I have the following code:
window.myobj = {
a: {_a: '_a here', _b: '_b here', _c: ''},
b: {_a: '_a here', _b: '_b here', _c: ''},
c: {_a: '_a here', _b: '_b here', _c: ''}
//etc.
}
myobj['a']._c = 'test';
alert(myobj['a']._c); // not test!
This works fine for the predefined values, but not dynamically-set ones.
EDIT:
I've narrowed it down to a loop issue - the key is always the last one in the array.
window.keys = Object.keys(servers);
for(var i = 0; i < keys.length; i++)
{
var key = keys[i];
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', function()
{
alert(key); // this is the issue due to this happening after the loop is finished
myobj[key]._c = this.responseText;
});
xhr.open('GET', 'test.php?s=' + myobj[key]._b);
xhr.send();
}
What I have tried:
Setting the value directly, using ['_c'] and setting myobj['a'] to a new object entirely.
The problem "the key is always the last one in the array" is indeed happening because the 'load' events only get fired after the loop is over, sokey
is indeed equal to the last key at the time you callalert(key)
. This problem can be solved by replacing youraddEventListener
call by this:
xhr.addEventListener('load', (function(k) { return function() { alert(k); myobj[k]._c = this.responseText; }; })(key));This looks a bit complicated, so let's break this piece of code down into its different parts.
(function(k) { ... })(key)
The above is a self-executing function. When the function executes itself, we pass
key
as thek
parameter:k
will be the key at the time thataddEventListener
got called and it will not be affected by changes to thekey
variable.
Inside the self-executing function, we return a new function. We need to do that because we do not want to runalert
(and the other line) immediately: we want to have a function that we can pass toaddEventListener
so that it runs on the 'load' event. So we have to return the function that we want to get fired on the 'load' event:
return function() { alert(k); myobj[k]._c = this.responseText; };We use
k
here and notkey
, because we want to use the variable inside the self-executing function that doesn't get affected bykey
changes.
这篇关于在JS对象中设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!