javascript - code-style: Is inline initialization of JS objects ok? -
I often get inline using Initializing (see the example below), especially in a switch statement when I Do not know which cases will be loop hits, I know that it is easy to read compared to the statement.
But is this a good practice or will it affect side-effect or performance?
for (var i in array) {var o = o? O: {}; // init object if it does not exist o [array [i]] = 1; // key-enter code} Is there a good website to get coding style suggestions?
Another commonly used pattern to do this, or = Use of operator (slightly more readable than your Ternary IMHO):
// ... var obj = o || {}; This operator will return to its second operand if it evaluates the first code false , else it will be the first one.
is safe when you expect something, to use it since, because duplicate value null , undefined < / Code>, NaN , 0 , a zero-length string, and of course false .
I used to be useful for setting default values on function arguments, when definitely expecting to be valid by any function of false values Goes:
function testing (arg1) {arg1 = arg1 || "default value"; // ..}
Comments
Post a Comment