4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

parseIntの基数問題

4
Last updated at Posted at 2017-10-01

この記事を読んで、基数の罠は分かりすぎると思いつつ、標準関数の上書きのキモさが気になったので、自分がやるならどうするか考えてみた。

関数を上書きするのではなく、基数を固定したラッパー関数として実装すれば意味としても明確になるし標準関数を汚染もしないし良いのではと思う。

function parseInt10(value) {
  return parseInt(value, 10);
}
function parseInt8(value) {
  return parseInt(value, 8);
}
function parseInt2(value) {
  return parseInt(value, 2);
}

ちなみに自分は標準関数をちゃんと基数付きで使えばいいじゃんと考えています。
色だと16進数だしビットフラグで2進数のときもあるし、文字から数値をパースするっていう用途に対してちゃんと設計されているので、
そのあたりの思想とか考え方をちゃんと把握しているほうがいいし、「オリジナルと違う」という気遣いが逆に余計な場合もあるので、素直にオリジナル使うのが一番よいのではと思った。

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?