hand assignment with JavaScript
var var1 = 1,
var2 = 1,
var3 = 1;
This is equivalent to this:
var var1 = var2 = var3 = 1;
I'm fairly certain this is the order the variables are defined: var3, var2, var1, which would be equivalent to this:
var var3 = 1, var2 = var3, var1 = var2;
Is there any way to confirm this in JavaScript? Using some profiler possibly?
Actually,
var var1 = 1, var2 = 1, var3 = 1;
is not equivalent to:
var var1 = var2 = var3 = 1;
The difference is in scoping:
function good() {
var var1 = 1, var2 = 1, var3 = 1;
}
function bad() {
var var1 = var2 = var3 = 1;
}
good();
console.log(window.var2); // undefined
bad();
console.log(window.var2); // 1. Aggh!
Assignment in javascript works from right to left. var var1 = var2 = var3 = 1;
.
If the value of any of these variables is 1
after this statement, then logically it must have started from the right, otherwise the value or var1
and var2
would be undefined.
You can think of it as equivalent to var var1 = (var2 = (var3 = 1));
where the inner-most set of parenthesis is evaluated first.
a = (b = 'string is truthy'); // b gets string; a gets b, which is a primitive (copy)
a = (b = { c: 'yes' }); // they point to the same object; a === b (not a copy)
(a && b)
is short for (a ? b : a)
(a || b)
is short for (a ? a : b)
(a = 0, b)
is short for not caring if a
is truthy, implicitly return b
a = (b = 0) && "nope, but a is 0 and b is 0"; // b is falsey + order of operations
a = (b = "b is this string") && "a gets this string"; // b is truthy + order of ops
JavaScript Operator Precedence (Order of Operations)
Note that the comma operator is actually the least privileged operator, but parenthesis are the most privileged, and they go hand-in-hand when constructing one-line expressions.
Eventually, you may need 'thunks' rather than hardcoded primitive values, and I've definitely gotten tripped up by accidentally passing multiple params, rather than running through two expressions.
To me, a thunk is both the function and the resultant value (the same 'thing').
const windowInnerHeight = () => 0.8 * window.innerHeight; // a thunk
windowInnerHeight(); // a thunk
链接地址: http://www.djcxy.com/p/1880.html
上一篇: 阅读数组的`length`属性真的很贵的JavaScript操作?
下一篇: 用JavaScript进行手工分配