This is similar to Python. The reason is quite simple: you cannot add this because it encounters a rule to define a search scope for searching non-local variables in external areas. This is possible in statically typed languages, since this members are known at compile time.
And what if it made this dynamic decision, for example, " x refers to this.x if this.x !== undefined and otherwise to the variable x " (or any other rule for this that is defined at run time) ? This is very dangerous, because it can shade local variables based on what this happens, that is, it breaks perfectly valid code only for certain objects. Another problem: should undeclaredVar = ... add a new instance attribute? If not, it will be the ugly asymmetry between implicit and explicit this . If it creates an instance attribute, you lose the ability to set a global and trailing variable from within the functions β not too many losses, many will say; but the JS developers seemed to think differently, because by default they chose a global scope.
Creating attributes of a shadow instance of "random variables" would be less dangerous, but with deeply nested areas filled with many names, in most cases you would have to use this. , therefore, fewer wins on the network. For this and / or perhaps for other reasons, the designers considered that reduction was impossible.
delnan Feb 02 2018-11-18T00: 00Z
source share