当前位置: 首页 > 文档资料 > Lazy.js 英文文档 >

define

优质
小牛编辑
140浏览
2023-12-01

Create a new constructor function for a type inheriting from Sequence.

Signature

Sequence.define = function(methodName, overrides) { /*...*/ }
Sequence.define = function define(methodName, overrides) {
  if (!overrides || (!overrides.getIterator && !overrides.each)) {
throw new Error("A custom sequence must implement *at least* getIterator or each!");
  }

  return defineSequenceType(Sequence, methodName, overrides);
}
NameType(s)Description
methodNamestring|Array.<string>

The name(s) of the method(s) to be used for constructing the new sequence. The method will be attached to the Sequence prototype so that it can be chained with any other sequence methods, like #map, #filter, etc.

overridesObject

An object containing function overrides for this new sequence type. Must include either getIterator or each (or both). May include an init method as well. For these overrides, this will be the new sequence, and this.parent will be the base sequence from which the new sequence was constructed.

returnsFunction

A constructor for a new type inheriting from Sequence.

Examples

// This sequence type logs every element to the specified logger as it
// iterates over it.
Lazy.Sequence.define("verbose", {
  init: function(logger) {
this.logger = logger;
  },

  each: function(fn) {
var logger = this.logger;
return this.parent.each(function(e, i) {
  logger(e);
  return fn(e, i);
});
  }
});

Lazy([1, 2, 3]).verbose(logger).each(Lazy.noop) // calls logger 3 times