define
优质
小牛编辑
136浏览
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); }
Name | Type(s) | Description |
---|---|---|
methodName | string|Array.<string> | The name(s) of the method(s) to be used for constructing the new sequence. The method will be attached to the |
overrides | Object | An object containing function overrides for this new sequence type. Must include either |
returns | Function | A constructor for a new type inheriting from |
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