admin管理员组文章数量:1181432
I am using a large JS library to perform certain drawing operations in canvas. Reviewing the library code (to make accommodating changes), I have ran into the ||
operator being used in a fashion which certainly should not evaluate to Boolean. Does this mean that this is a different operator or am I missing something obvious? An example follows:
var $time = Date.now || function() {
return +new Date;
};
I am using a large JS library to perform certain drawing operations in canvas. Reviewing the library code (to make accommodating changes), I have ran into the ||
operator being used in a fashion which certainly should not evaluate to Boolean. Does this mean that this is a different operator or am I missing something obvious? An example follows:
var $time = Date.now || function() {
return +new Date;
};
Share
Improve this question
edited Oct 8, 2024 at 18:27
dumbass
27.2k4 gold badges36 silver badges73 bronze badges
asked Sep 4, 2009 at 11:13
KJ SaxenaKJ Saxena
21.8k24 gold badges84 silver badges111 bronze badges
3
|
5 Answers
Reset to default 17There is already an accepted answer, but I like to mention, that the OR-Operator is also called Default-Operator, because it doesn't return a boolean, but instead the left or right hand argument.
Same goes for the AND-Operator, which is also called guard-Operator.
Check out crockford's Survey of the JavaScript Programming Language for more details:
The && operator is commonly called logical and. It can also be called guard. If the first operand is false, null, undefined, "" (the empty string), or the number 0 then it returns the first operand. Otherwise, it returns the second operand. This provides a convenient way to write a null-check:
var value = p && p.name; /* The name value will only be retrieved from p if p has a value, avoiding an error. */
The || operator is commonly called logical or. It can also be called default. If the first operand is false, null, undefined, "" (the empty string), or the number 0, then it returns the second operand. Otherwise, it returns the first operand. This provides a convenient way to specify default values:
value = v || 10; /* Use the value of v, but if v doesn't have a value, use 10 instead. */
The ||
operator evaluates to the first operand if it can be converted to true or the last operand otherwise. So in your example $time
will be Date.now
if it exists or the declared function otherwise.
I believe that code is saying if the DateTime.now
variable does not exist, instead return the result of +new Date
.
This is very common is javascript. If Data.now evaluates to true, then $time is set to that, else it is set to the function.
The ||
operator is being used to assign a value to $time
in the example.
If Date.now
evaluates to false, then $time
is assigned the value on the right side of the ||
operator (in this case, a function). If Date.now
evaluates to true, then it short-cicuits and assigns the value of Date.now to $time
本文标签:
版权声明:本文标题:javascript - What does the || (logical OR, double vertical line) operator do with non-boolean operands? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738219482a2069939.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Date.now
variable to know fully how this would work, then? – peirix Commented Sep 4, 2009 at 11:21