开发者

Are there more short and tricky codes in javascript? [closed]

开发者 https://www.devze.com 2023-03-07 12:40 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this
Closed. This question needs to be more focused. It is not currently accepting answers.

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 question

I 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 not

Math.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/

0

精彩评论

暂无评论...
验证码 换一张
取 消