Giter Club home page Giter Club logo

Comments (8)

azu avatar azu commented on July 1, 2024

書いていて Boolean によるキャストは殆ど書いた記憶ない。( !! の方が見かけるぐらい)
キャストをしたい場合は明示的に真偽値が欲しい時になるはずだけど、
これはBooleanのキャスト = #53 型変換 を悪用してるような気がする。

空文字なら 文字列.length === 0 などの比較演算子を使えばいいし、キャストが必要になるケースは大抵がおかしな状態になってる気がする。
なので、それを使うケースが出てきたら危険信号 ⚠️ であるという話もできるといい気がする。

from js-primer.

teppeis avatar teppeis commented on July 1, 2024

思い出してみる。

  • nullable string型に対して1文字以上あるかどうか
  • nullable boolean型をnon-nullableにする
  • ビット演算後にビットが立ってるかどうか

いずれも比較演算子で書けるので、危険信号と言えなくもない。
ただ、nullable系は2段階比較が必要だとつらいのでBoolean()か!!したい。
その場合Boolean()と!!がどっちがいいかと言うとどっちでもいい気がする。

from js-primer.

azu avatar azu commented on July 1, 2024

ただ、nullable系は2段階比較が必要だとつらいのでBoolean()か!!したい。

https://twitter.com/mysticatea/status/749523213244207104
こういう感じのもの?

if( str == null ) {
    return false;
}
if( str.length === 0 ){
    return false
}
// == ?
return Boolean(str);

from js-primer.

teppeis avatar teppeis commented on July 1, 2024

はい、そです。意味としてはa.length > 0なんだけど、nullableだと先にnullチェックがいるっていう。
柔らかいJSだとnon-nullableという確証がないので常に!!aとかBoolean(a)の方が安全ということになりがち。

ClosureやFlowtypeだと、型をnon-nullableに寄せれば躊躇せずa.length > 0だけでOKなんだけど。

from js-primer.

azu avatar azu commented on July 1, 2024

このセクションはやっぱりいらない感じがするなー。
#53 で大体カバーできるので、 Boolean オブジェクトを知っていることで何か得する事があるかどうかぐらいかな。

問題としては、タイトル的に falsy が書いてある場所が探しにくい感じぐらいかな。
(falsy自体については多分いろんな所で何度も説明書く感じ。気をつける必要があるものだし)

from js-primer.

azu avatar azu commented on July 1, 2024

[].filter(Boolean) という使い方してるケース

https://github.com/babel/babel/pull/3673/files#diff-f0dba020c48e5f7861c241eb8ac5aa8eR7

from js-primer.

azu avatar azu commented on July 1, 2024

これは結局多分章としてはない気がする

from js-primer.

azu avatar azu commented on July 1, 2024

これは章としてはいらないのでclosed

#328 (comment)

from js-primer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.