2
3

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.

underscoreコードリーディング(defaults)

Posted at

underscoreに詳しくないので、勉強半分でソースコードを読む。

##利用するバージョン
underscore.js(v1.8.3)

##defaultsとは

###underscorejs.orgのdefaults

こんな説明。

####_.defaults(object, *defaults)
Fill in undefined properties in object with the first value present in the following list of defaults objects.

var iceCream = {flavor: "chocolate"};
_.defaults(iceCream, {flavor: "vanilla", sprinkles: "lots"});
=> {flavor: "chocolate", sprinkles: "lots"}


defaults objectsに指定されている値で、オブジェクトのundefinedなプロパティを埋めます。

###underscore.defaults
コード的にはこのあたり。

  // Fill in a given object with default properties.
  _.defaults = createAssigner(_.allKeys, true);

createAssignerに_.allKeysとtrueを渡す。
createAssigner_.extendで説明したとおり、objectを引数にとる関数で、_.allKeysの結果を用いて、obj[key] === void 0なところに値を埋める。

2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?