There is a distinction in the second scenario where you are attempting to assign a value to a variable named properties
without first declaring it. The use of var
only pertains to prop
, not properties
, as properties
appears on the right side of the assignment operator (=
) following prop
, making it part of the initialization process for prop
. Here is how it breaks down:
var prop = (properties = this.properties);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^−−−− initializer for `prop`
In non-strict mode, this leads to what I refer to as The Horror of Implicit Globals — creating a global variable called properties
. When using strict mode, assigning to an undeclared identifier results in the error it should always have been. Hence, all new code should adhere to strict mode.
You can declare multiple variables with var
, but you cannot assign them identical values without repeating the source.
You could opt for:
var prop, properties;
prop = properties = this.properties;
If preferred. Alternatively:
var prop = this.properties, properties = prop;
This approach works because initializers within a var
(or let
or const
) involving multiple declarations are executed based on the order in the source code, from left to right. Thus, prop = this.properties
executes before properties = prop
.
Note: It is advisable to avoid using var
in new code and instead opt for let
or const
. If there is a need to support outdated JavaScript engines, modern code can be transpiled to ES5 standards.