I am trying to declare 2 variables to the same value ( false ).
I tend to do this all over the place ( like, in almost every object I prototype )
var $a= {border:false
,frame:false};
or alternately
border=false;frame=false;
Is there better way I can declare the value of both simultaneously? ( eg a code golf solution )
You can, if both variables are declared when you set the value. This is kind of quirky, but it works:
(this would be the shortest you can get I think)
Is it better? I don’t think so, and you don’t really save much. More readable is
Actually there some more combinations you could do, but you cannot do it without repeating either a variable name or the value. E.g. you could also do:
Of course this only works for primitive values (i.e. booleans, (literal) numbers, (literal) strings). If you deal with objects (which includes arrays), then both variables would reference the same object. In this case you really have to initialize them separately:
Update, because I feel the need to explain why
var border = frame = false;does not work:varis not transitive here and this expression is actually evaluated from right to left. First,falseis assigned toframe, which will be looked up in the scope chain and in the worst case will become global. Then the value offrameis assigned to the local variableborder.