Search code examples
javascriptif-statementchaining

Is line 4 inside the if statement started on line 2?


I'm trying to learn about chaining and how to do it myself by reading OPC. I'm still very stuck. While learning this, I ran into a bit of error handling that raised my eyebrows. It seems, from the indentation, that line 4 is not a part of the if statement that begins on line 2. Am I right to assume that 2-line if statements do not require curly braces, and the interpreter assumes that when there is a lack of curly braces, only the line directly proceeding an if statement is a part of that same if statement; therefore, the fourth line is not a part of the if statement in the following block:

    set: function (mystr, func) {
        if (!this[mystr])
            throw new Error("unknown hook " + mystr);
        this[mystr] = func;
    },

Solution

  • It's typically not the line but the statement succeeding the if-statement that's considered belonging with it.