これは、「塹壕からの経験」の質問です。
このJavaScriptの断片を考えると
/**
* @param [foo]
* {Object} an optional object (it can be null, undefined, empty, etc..)
* @param [foo.bars]
* {Array} an Array that *might* be in the object
*/
function (foo) {
// I want to get the array, or an empty array in any
// of the odd cases (foo is null, undefined, or foo.bars is not defined)
var bars = [];
if (foo && foo.bars) {
bars = foo.bars
}
// ....
}
これを短くしようとしています。MDNによると、次のように記述しても問題ありません。
function (foo) {
var bars = (foo && foo.bars) || [];
// ...
}
これが機能しないケース (値のセット、または他のブラウザー) がありませんか? これを行うためのより短い/よりクリーンな方法はありますか?
より主観的なノードでは、それを読めないと思いますか?
ありがとう