ali*_*noi 1 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) => {
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;
}
Run Code Online (Sandbox Code Playgroud)
我的 Firefox 是44.0,它SyntaxError为这些行抛出一个:
if (key < this.key) {
[this, parent] = [this.lft, this];
} else {
Run Code Online (Sandbox Code Playgroud)
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];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20206 次 |
| 最近记录: |