JavaScript:无效的解构目标

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)

我试图通过阅读这篇博文MDN来了解这里到底出了什么问题。不幸的是,我仍然想念它:(

Ber*_*rgi 5

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)