问题
I'm working with this JS plugin, and I've encountered some syntax I've never seen before. I understand what it's doing, but I'm not sure why it works.
Here's an example of one instance of it:
settings.maxId != null && (params.max_id = settings.maxId);
Is this just taking advantage of conditionals and the single = ? Is this common syntax for JS?
回答1:
In JavaScript the =
operator is an expression and evaluates the assigned value. Because it is an expression it can be used anywhere an expression is allowed even though it causes a side-effect.
Thus:
settings.maxId != null && (params.max_id = settings.maxId)
Means: If settings.maxId
is not null then (and only then, since &&
is short circuiting) evaluate the right-expression (params.max_id = settings.maxId
) which in turn causes the value of settings.maxId
to be assigned to params.max_id
.
This is much more clearly written as:
if (settings.maxId != null) {
params.max_id = settings.maxId
}
Happy coding.
回答2:
The &&
operator is known as "boolean AND
". Typically, you'd see it in an if
statement:
if (x == true && y == false) {
but that's not a restriction. You may use it in any valid expression to "combine" the boolean values of its operands into a single boolean result, according to the logical "AND" operation:
var z = (x == true && y == false);
// z is now true or false, accordingly
One of the lovely things about &&
is that it "short circuits". In false && true
, because the first operand is false
the entire expression may only evaluate to false
, so the second operand is not even evaluated.
Let's check that again:
var z = (false && foo());
// z is now false
In this statement, the function foo
is never even called! It doesn't have to be, for the program to know that z
will be false.
This is more than an optimisation — you can rely on it.
Some silly people use this technique to rewrite conditional statements:
if (x == 0) {
foo();
}
into hard-to-read single expressions:
(x == 0) && foo();
Now, consider that assignment can be an expression just like a function call:
var a = (b = c);
Or:
var a = (b = foo());
And add in a conditional via the above technique:
var a = ((x == 0) && (b = foo()));
Now the entire expression b = foo()
won't be evaluated at all if x
is not 0
, because of short circuiting.
We don't even need to do anything with the result of the &&
operation, and if we don't store it to a
you're left with just:
(x == 0) && (b = foo());
which is a statement that'll assign b
to the value of foo()
only if x
is 0
.
Avoid it. It's hard to read. Just use an if
statement.
回答3:
this statement will assign params.max_id = settings.maxId
only if settings.maxId != null
due to the fact that &&
is a short-circuit logic operator
this behaviour is due to the fact that javascript will evaluate the condition until it's necessary. thus, if first condition is false and the second is in AND
there's no need to check further
来源:https://stackoverflow.com/questions/9726496/what-is-this-js-syntax-assignment-in-expression-x-null-y-x