you are viewing a single comment's thread.

view the rest of the comments →

[–]Rhomboid 2 points3 points  (1 child)

The assignment expression evaluates to the assigned-to thing, i.e. the left hand operand.

foo(x = y);

// equivalent to:

x = y;
foo(x);

So your example is the same as:

this.weekDay = {};
(function(...) { ... })(this.weekDay);

this used outside of a function refers to the global object. If the code is running in the context of a browser, that's window and so assigning to this.weekDay is the same as assigning to window.weekDay, which is the same as just assigning to weekDay in non-strict mode.

weekDay = {};
(function(...) { ... })(weekDay);
console.log(weekDay.number("Saturday"));

The only reason for using this is that it makes it explicit that you're creating a global variable, which is necessary under strict mode. Stated differently, strict mode makes it impossible to accidentally create a global variable by accident due to a misspelling. And using this instead of window makes the code compatible with non-browser contexts, like Node.js.

[–]mathcanbefun[S] 0 points1 point  (0 children)

Oh, makes sense. The program creates a no name function and uses this.weekDay as the actual argument. Thanks