gpt4 book ai didi

JavaScript:无效的解构目标

转载 作者:行者123 更新时间:2023-11-30 09:53:18 25 4
gpt4 key购买 nike

代码如下:

 function BinarySearchNode(key) {
let node = {};
node.key = key;
node.lft = null;
node.rgt = null;

node.log = () => {
console.log(node.key);
}

node.get_node_with_parent = (key) => {
let parent = null;

while (this) {
if (key == this.key) {
return [this, parent];
}

if (key < this.key) {
[this, parent] = [this.lft, this];
} else {
[this, parent] = [this.rgt, this];
}
}

return [null, parent];
}

return node;
}

我的 Firefox 是 44.0,它会为这些行抛出一个 SyntaxError:

if (key < this.key) {
[this, parent] = [this.lft, this];
} else {

我试图通过阅读 this blogpost 来理解这里到底出了什么问题和 the MDN .不幸的是,我仍然想念它:(

最佳答案

this不是变量,而是关键字,不能赋值。改用变量:

node.get_node_with_parent = function(key) {
let parent = null;
let cur = this; // if you use an arrow function, you'll need `node` instead of `this`
while (cur) {
if (key == cur.key) {
return [cur, parent];
}
if (key < cur.key) {
[cur, parent] = [cur.lft, cur];
} else {
[cur, parent] = [cur.rgt, cur];
}
}
return [null, parent];
}

关于JavaScript:无效的解构目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35255536/

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