4



Double bitwise NOT (~~) - James Padolsey http://james.padolsey.com/javascript/double-bitwise-not/
Web

Reflection: Two simple tricks in JavaScript ( olds, but always便利です)
http://webreflection.blogspot.com/2008/06/two-simple-tricks-in-javascript-olds.html


double bitwise not

Math.round(v)=== ~~v

Math.floor(v)=== ~~v(v > 0 の場合)

isNaN(Number(v)) ? 0 : Number(v)=== ~~v(v の場合) float ではありません)


double not

Boolean(v)=== !!v

( !Boolean(v)=== !v)

bitwise shift

Math.round(v / 2)=== v >> 1

Math.round(v)=== v >> 0


single bitwise not

a.indexOf(v) !== -1=== ~a.indexOf(v)


javascript にはもっと短いコードやトリッキーなコードがありますか?

4

1 に答える 1

4

これらの「トリック」は Javascript に固有のものではありません。Google で簡単に検索すると、同様のトリックを提供する多数のページが返されます。

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/

于 2011-05-23T03:41:33.460 に答える