Reputation: 5101
How does this style of variable assignment (it's inside an object so the "this" variable refers to said object)):
var prop = this.properties;
var properties = this.properties;
Differ from this?
var prop = properties = this.properties;
When I changed to the latter it caused issues. Is it a referencing issue?
Upvotes: 2
Views: 136
Reputation: 1074355
The difference is that with the second one, you're trying to assign to a properties
variable that you haven't declared. The var
only applies to prop
, not properties
because properties
is on the right-hand side of the =
after prop
, so it's part of the initializer for prop
. Here's how it's grouped:
var prop = (properties = this.properties);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^−−−− initializer for `prop`
In loose mode, that means you're falling prey to what I call The Horror of Implicit Globals — it creates a global called properties
. In strict mode, assigning to an undeclared identifier is the error it always should have been. (All new code should be written using strict mode.)
You can declare multiple variables with var
, etc., but you can't assign to them all using the same value source (without repeating it).
You could do:
var prop, properties;
prop = properties = this.properties;
if you liked. Or alternatively:
var prop = this.properties, properties = prop;
That works because the initializers in a var
(or let
or const
) with multiple declarations are executed in source code order, left-to-right, so prop = this.properties
happens before properties = prop
does.
Side note: var
shouldn't be used in new code, use let
or const
. (If you have to support obsolete JavaScript engines, you can transpile modern code to ES5.)
Upvotes: 4