JavaScript:无效的分解目标
代码如下:JavaScript:无效的分解目标,javascript,syntax-error,Javascript,Syntax Error,代码如下: 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) => { l
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;
}
我试图通过阅读和阅读来理解这里到底出了什么问题。不幸的是,我仍然缺少它:(不是一个变量,而是一个关键字,无法分配给。请改用变量:
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];
}
node.get\u node\u与\u parent=函数(键){
让parent=null;
设cur=this;//如果使用arrow函数,则需要'node'而不是'this'`
while(cur){
如果(键==当前键){
返回[cur,parent];
}
如果(键<当前键){
[cur,parent]=[cur.lft,cur];
}否则{
[cur,parent]=[cur.rgt,cur];
}
}
返回[null,parent];
}
不是变量,而是关键字,无法分配给。请改用变量:
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];
}
node.get\u node\u与\u parent=函数(键){
让parent=null;
设cur=this;//如果使用arrow函数,则需要'node'而不是'this'`
while(cur){
如果(键==当前键){
返回[cur,parent];
}
如果(键<当前键){
[cur,parent]=[cur.lft,cur];
}否则{
[cur,parent]=[cur.rgt,cur];
}
}
返回[null,parent];
}
这难道不意味着你在试图将对象从自身下分配出去吗?@DaveNewton老实说,我不知道。目前我的理解是,这个
就像python中的self
一样:只是一个引用(一个名称,一个指针)对于对象。所以,如果我将它重新分配给另一个对象,我很好。也许不是。不是那么多。它是对一个对象的引用,但对象本身。将自己设置为不同的对象意味着什么?经常使用的电影情节,代码不是很热门。这难道不意味着你正试图从对象本身下面分配对象吗?@DaveNewton no ide老实说,目前我的理解是,this
就像python中的self
:只是一个引用(一个名称,一个指针)对于对象。所以,如果我将它重新分配给另一个对象,我很好。也许不是。不是那么多。它是对一个对象的引用,但对象本身。将自己设置为不同的对象意味着什么?经常使用的电影情节,代码不那么热门。谢谢,我看到了你关于节点
和此
的评论,我现在是班吉因为这个作用域/闭包/不知道正确调用了什么。如果在箭头函数中使用this
,它将不会引用函数(方法)中的对象在上调用,但与其父作用域的this
相同-在您的情况下,是BinarySearchNode
的this
。如果它是构造函数(并使用new
调用),这是有道理的,但目前它只是一个工厂函数,您需要通过闭包来引用节点
。谢谢,我看到您对节点
和这个
的评论,我现在有点头痛,因为这个范围/闭包/不知道它的正确名称。如果您在arr中使用这个
对于函数,它不会引用调用函数(方法)的对象,而是引用与其父作用域相同的this
——在您的例子中,是BinarySearchNode
的this
。如果它是构造函数(并使用new
调用),这是有道理的,但目前它只是一个工厂函数,您需要通过闭包引用节点。