Monday, May 20, 2024
85
rated 0 times [  88] [ 3]  / answers: 1 / hits: 27096  / 8 Years ago, sun, february 7, 2016, 12:00:00

Here is the code:



 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;
}


My Firefox is 44.0 and it throws a SyntaxError for these lines:



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


I tried to understand what exactly is wrong here by reading this blogpost and the MDN. Unfortuntely, I am still missing it :(


More From » syntax-error

 Answers
36

this is not a variable, but a keyword and cannot be assigned to. Use a variable instead:



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];
}

[#63405] Friday, February 5, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pranavrorys

Total Points: 466
Total Questions: 87
Total Answers: 115

Location: Barbados
Member since Sun, Nov 27, 2022
2 Years ago
pranavrorys questions
Fri, May 27, 22, 00:00, 2 Years ago
Thu, Oct 28, 21, 00:00, 3 Years ago
Sat, May 30, 20, 00:00, 4 Years ago
Fri, Dec 20, 19, 00:00, 5 Years ago
;