Comments (8)
書いていて Boolean
によるキャストは殆ど書いた記憶ない。( !!
の方が見かけるぐらい)
キャストをしたい場合は明示的に真偽値が欲しい時になるはずだけど、
これはBooleanのキャスト = #53 型変換 を悪用してるような気がする。
空文字なら 文字列.length === 0
などの比較演算子を使えばいいし、キャストが必要になるケースは大抵がおかしな状態になってる気がする。
なので、それを使うケースが出てきたら危険信号
from js-primer.
思い出してみる。
- nullable string型に対して1文字以上あるかどうか
- nullable boolean型をnon-nullableにする
- ビット演算後にビットが立ってるかどうか
いずれも比較演算子で書けるので、危険信号と言えなくもない。
ただ、nullable系は2段階比較が必要だとつらいのでBoolean()か!!したい。
その場合Boolean()と!!がどっちがいいかと言うとどっちでもいい気がする。
from js-primer.
ただ、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.
はい、そです。意味としてはa.length > 0
なんだけど、nullableだと先にnullチェックがいるっていう。
柔らかいJSだとnon-nullableという確証がないので常に!!a
とかBoolean(a)
の方が安全ということになりがち。
ClosureやFlowtypeだと、型をnon-nullableに寄せれば躊躇せずa.length > 0
だけでOKなんだけど。
from js-primer.
このセクションはやっぱりいらない感じがするなー。
#53 で大体カバーできるので、 Boolean
オブジェクトを知っていることで何か得する事があるかどうかぐらいかな。
問題としては、タイトル的に falsy が書いてある場所が探しにくい感じぐらいかな。
(falsy自体については多分いろんな所で何度も説明書く感じ。気をつける必要があるものだし)
from js-primer.
[].filter(Boolean)
という使い方してるケース
https://github.com/babel/babel/pull/3673/files#diff-f0dba020c48e5f7861c241eb8ac5aa8eR7
from js-primer.
これは結局多分章としてはない気がする
from js-primer.
これは章としてはいらないのでclosed
from js-primer.
Related Issues (20)
- Math: "次の" をとる
- P369: `https://api.github.com/users/GitHubユーザーID` のコード処理
- P395: 脚注がMarkdownのコードになっている
- P369: セクションタイトルがリンクになっている
- P407: "31.5.1 アプリケーションをモジュールに分割する"がリンクになっている
- TODOアプリ: EventTargetのNode.jsについて
- TODOアプリ: Viewの対応関係の強調
- P471: 索引の .at がリンクになっている HOT 1
- Publish: 細かいレイアウト崩れ
- P416: file:/// がリンクになっている
- anonymous functionの訳語について(匿名関数 or 無名関数) HOT 1
- 脱字の報告
- TodoItemView、TodoListViewのJSDocのparamについて HOT 1
- ECMAScript 2023の対応 HOT 10
- Publish: npm 8 → npm 9 HOT 1
- Publish: 非同期の図がモノクロだと違いがわからない HOT 4
- ES2023: proposal-array-find-from-last HOT 1
- ES2023: Symbol as a Key HOT 1
- ES2023: Change Array by copy HOT 8
- website: 書籍へのリンクを改訂2版に変更する HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from js-primer.