3 ms·
> return err; // breaks, since err is spooky and only "scoped" in the catch block Both err's? (the argument and the one you define)? How about: try {
by e12e 5y ago
> return err; // breaks, since err is spooky and only "scoped" in the catch block
Both err's? (the argument and the one you define)?
How about:
try {
throw "error";
} catch (err) {
var e = err; // is this different from not shadowing err?
console.log(e)
} finally {
return e;
}
- sangeeth96 5y agoOP didn't redefine `err`, just assigned a new value to the `err` variable that was available as `catch`'s parameter. Only the catch's error variable is scoped to the catch block. Any variable that's defined inside `catch` block using `var` is hoisted in the scope of the nearest function. So, the `return e` in your example works since `e` was hoisted when you defined it using `var e = err`. If you tried `return err` instead, it'd result in a `ReferenceError`.
- e12e 5y ago> OP didn't redefine `err`, just assigned a new value to the `err` variable that was available as `catch`'s parameter. How would you define "redefine" if this isn't an example of redefining? > the `return e` in your example works since `e` was hoisted when you defined it using `var e = err`. If you tried `return err` instead, it'd result in a `ReferenceError`. Ok, thank you.