gpt4 book ai didi

Javascript代理: Modify array target and update array (length,内容)

转载 作者:行者123 更新时间:2023-11-28 05:40:11 24 4
gpt4 key购买 nike

我正在代理一个数组并根据某些条件修改该数组以保持数据完整性,但从代理修改该数组时效果不佳。

我遇到的问题是代理数组被 ***undefined*** 污染。

如果目标对象被修改,如何更新它以使其正确?

这是一支笔示例:http://codepen.io/anon/pen/ZOPLZg (如果您查看控制台,空值实际上是未定义的值)。

使用 Chrome 版本 51.0.2704.103(64 位)

var test = new Proxy([], {
set: function(target, name, value, receiver) {
if(name != 'length'){

console.log(name, target)

// I want to overwrite values
var i = target.indexOf(value)
if(i > -1){
// doesn't work
target.splice(parseInt(name, 10), 1);

// doesn't work
delete target[name];

target[i] = value;
}else{
target[name] = value;
}
}else{
target[name] = value;
}
console.log(target);
return true;
}
});

function log(){
$('.log').append('<br/>' + JSON.stringify(test) + test.length);
}

log();
test.push('what', 'hi', 'yo');
log();
test.push('what', 'yo');
log();
test.splice(0, test.length)
log();

在此处提交错误 https://bugs.chromium.org/p/chromium/issues/detail?id=638414#

最佳答案

if (name != 'length')
{
// ...
} else {
// Your trouble is the next line. Comment it to test
// But i don't understand why.
target[name] = value;
}

关于Javascript代理: Modify array target and update array (length,内容),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38982598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com