什么是下划线功能?

时间:2013-05-04 20:24:53

标签: javascript underscore.js

我一直在使用下划线作为静态的集合。

underscore函数是什么:

var _ = function(obj) {
    if (obj instanceof _) return obj;
    if (!(this instanceof _)) return new _(obj);
    this._wrapped = obj;
  };

您将如何使用它的示例是什么?

2 个答案:

答案 0 :(得分:4)

用它包装对象:

_([1, 2, 3, 4]);

然后在包装对象上使用Underscore函数:

_([1, 2, 3, 4]).shuffle()

答案 1 :(得分:4)

您还可以使用Underscore作为包装函数,以获得更多OOP-like style

_(val).method(…);
// instead of the equal
_.method(val, …);

这些包装器对象也允许chaining

_.chain(val).method1(…).method2(…);
// or
_(val).chain().method1(…).method2(…);
相关问题