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

define

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

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

Signature

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

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

overridesObject

An object containing function overrides for this new sequence type. Has the same requirements as ArrayLikeSequence.define.

returnsFunction

A constructor for a new type inheriting from StringLikeSequence.

Examples

Lazy.StringLikeSequence.define("zomg", {
  length: function() {
return this.parent.length() + "!!ZOMG!!!1".length;
  },

  get: function(i) {
if (i < this.parent.length()) {
  return this.parent.get(i);
}
return "!!ZOMG!!!1".charAt(i - this.parent.length());
  }
});

Lazy('foo').zomg() // sequence: "foo!!ZOMG!!!1"