0
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.

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

Posted at

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

利用するバージョン

underscore.js(v1.8.3)

uniqueIdとは

underscorejs.orgのuniqueId

こんな説明。

####_.uniqueId([prefix])
Generate a globally-unique id for client-side models or DOM elements that need one.
If prefix is passed, the id will be appended to it.


_.uniqueId('contact_');
=> 'contact_104'


クライアントサイドのモデルやDOMで必要なグローバルなユニークIDを生成します。
prefixが渡された場合、それをIDの前につけます

underscore.uniqueId

コード的にはこのあたり。

  // Generate a unique integer id (unique within the entire client session).
  // Useful for temporary DOM ids.
  var idCounter = 0;
  _.uniqueId = function(prefix) {
    var id = ++idCounter + '';
    return prefix ? prefix + id : id;
  };

idCounterを0で定義します。
idをidCounterを前置インクリメントしたものをStringで代入します。
prefixがある場合はprefix+idを、ない場合はidのみを返します。

0
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
0
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?