JavaScript:无效的解构目标

时间:2016-02-07 15:48:10

标签: 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;
 }

我的Firefox为44.0,它会为这些行抛出SyntaxError

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

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

1 个答案:

答案 0 :(得分:2)

this不是变量,而是关键字,无法分配。改为使用变量:

-doSomethingWithCallback: