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

define

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

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

Signature

ArrayLikeSequence.define = function(methodName, overrides) { /*...*/ }
ArrayLikeSequence.define = function define(methodName, overrides) {
  if (!overrides || typeof overrides.get !== 'function') {
throw new Error("A custom array-like sequence must implement *at least* get!");
  }

  return defineSequenceType(ArrayLikeSequence, 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 ArrayLikeSequence prototype so that it can be chained with any other methods that return array-like sequences.

overridesObject

An object containing function overrides for this new sequence type. Must include get. May include init, length, getIterator, and each. For each function, this will be the new sequence and this.parent will be the source sequence.

returnsFunction

A constructor for a new type inheriting from ArrayLikeSequence.

Examples

Lazy.ArrayLikeSequence.define("offset", {
  init: function(offset) {
this.offset = offset;
  },

  get: function(i) {
return this.parent.get((i + this.offset) % this.parent.length());
  }
});

Lazy([1, 2, 3]).offset(1) // sequence: [2, 3, 1]