Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this questionI found the following short and tricky codes on
Double bitwise NOT (~~) - James Padolsey http://james.padolsey.com/javascript/double-bitwise-not/ Web Reflection: Two simple tricks in JavaScript ( olds, but always useful ) http://webreflection.blogspot.com/2008/06/two-simple-tricks-in-javascript-olds.html double bitwise notMath.round(v)
=== ~~v
Math.floor(v)
=== ~~v
(if v > 0)
isNaN(Number(v)) ? 0 : Number(v)
=== ~~v
(if v is not float)
double not
Boolean(v)
=== !!v
(!Boolean(v)
=== !v
)
bitwise shift
Math.round(v / 2)
=== v >> 1
Math.round(v)
=== 开发者_如何学Gov >> 0
single bitwise not
a.indexOf(v) !== -1
=== ~a.indexOf(v)
Are there more short or tricky codes in javascript?These "tricks" are not specific to Javascript. A simple search on Google will return a number of pages offering tricks similar.
http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html
http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/
http://www.beyond3d.com/content/articles/8/
精彩评论