SoFunction
Updated on 2025-04-14

Prototype ObjectRange object learning


function toArray() {
return ();
}

//======> ()

//We noticed that the map method is mapped to the collect method when returning
return {
//...
collect: collect,
map: collect,
//...
}

//======> collect()

//In this example, this method is actually equivalent to returning an array, because the parameters passed in are undefined. There is a method here, continue to view
function collect(iterator, context) {
iterator = iterator || ;
var results = [];
(function(value, index) {
((context, value, index));
});
return results;
}

//======> ()

//I finally saw this._each, and now I understand why the _each method is rewritten in ObjectRange. This method is used during traversal
function each(iterator, context) {
var index = 0;
try {
this._each(function(value) {
(context, value, index++);
});
} catch (e) {
if (e != $break) throw e;
}
return this;
}

//======> this._each()

//Describe this._each() in detail
//The key is the succ() method, because this method is used in _each to generate the next value.
//Where is this succ()? This method is defined in and
function _each(iterator) {
var value = ;
while ((value)) {
iterator(value);
value = ();
}
}

//I won’t talk about the following two methods.

//======> ()

function succ() {
return (0, - 1) +
(( - 1) + 1);
}

//======> ()

function succ() {
return this + 1;
}

//To sum up, if you want to define other types of ObjectRange objects, such as the Date type, then you have to implement the succ() method yourself to generate continuous objects