0
let registerDropdownHandler = (options, handler) => {
    this.element.addEventListener('contextmenu', (e) => {
        e.preventDefault();
        e.stopPropagation();

        let options = options;
        if (this.additionalDropdownOptions) options.push(...this.additionalDropdownOptions);

        let dropdown = openDropdown(e.clientX, e.clientY, options);
        dropdown.onselect = function(name) {
            handler(name);
            if (this.additionalDropdownHandler) this.additionalDropdownHandler(name);
        };
    });
};

Yes, I am aware the statement let options = options is redundant here, but why would it throw? It throws "Uncaught ReferenceError: options is not defined".

DavidsKanal
  • 655
  • 11
  • 15
  • Are you certain options defined? Try putting this right before the assignment: console.log("options is: ", options) – anderspitman Jan 23 '19 at 22:55
  • 3
    `let options` creates a binding in the current scope that is **not initialized**. This shadows the `options` **argument**. `= options;` tries to read that same beinding (i.e. itself), but because it is not initialized you get an error. That's called the **temporal dead zone**. A more obvious example of that would be `console.log(options); let options = 42;`. In other words: You cannot have an parameter and a variable with the same name and be able to reference both of them. If you used `var` instead, you wouldn't get an error but `options` would be `undefined` and thus fail somewhere else. – Felix Kling Jan 23 '19 at 22:56
  • So the error is a good thing, it tells you that you did something wrong. – Felix Kling Jan 23 '19 at 23:00
  • Alright, thanks, that cleared it up for me. I'm kind annoyed this question was marked as a 'duplicate'. I didn't ask for the temporal dead zone. Just because my question can be answered with a different question doesn't mean it's a duplicate of that question. There's a clear difference between the two. I would have more appreciated if a reference to that question was dropped here as an answer. – DavidsKanal Jan 23 '19 at 23:02

0 Answers0