You do not need if (context) {
at all. Fundamentally, you need to make the same operation regardless of whether or not a context is provided, so you can just leave the code outside this if
and always execute .call(context, /* other arguments */
. Function#call
will always execute the function and the first parameter will be the this
value of that invocation. If the value undefined
is provided, then that's not an error - in non-strict mode the value of this
will be assigned to the global object but in strict
mode it How does the "this" keyword work?
Thus, if a context is not provided, then you usually don't have to do anything special.
const _ = {};
_.each = function (collection, iteratee, context) {
//check if array or object
if (Array.isArray(collection)) {
for (let i = 0; i < collection.length; i++) {
iteratee.call(context, collection[i], collection);;
}
} else {
for (const key in collection) {
iteratee.call(context, collection[key], collection);
}
}
return collection;
};
const arr = [1, 2, 3];
const obj = {
foo: "hello",
bar: "world"
};
function print(value) {
console.log(value);
}
console.log("--- basic foreach ---");
_.each(arr, print);
_.each(obj, print);
console.log("--- foreach with context ---");
function contextPrint(value) {
console.log(this[value]);
}
_.each(arr, contextPrint, { 1: "one", 2: "two", 3: "three" });
_.each(obj, contextPrint, { hello: "this", world: "works" });
console.log("--- no context provided ---");
_.each(arr, contextPrint);
_.each(obj, contextPrint);
Compare with Underscore and the behaviour is the same:
const arr = [1, 2, 3];
const obj = {
foo: "hello",
bar: "world"
};
function print(value) {
console.log(value);
}
console.log("--- basic foreach ---");
_.each(arr, print);
_.each(obj, print);
console.log("--- foreach with context ---");
function contextPrint(value) {
console.log(this[value]);
}
_.each(arr, contextPrint, { 1: "one", 2: "two", 3: "three" });
_.each(obj, contextPrint, { hello: "this", world: "works" });
console.log("--- no context provided ---");
_.each(arr, contextPrint);
_.each(obj, contextPrint);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.11.0/underscore-min.js" integrity="sha512-wBiNJt1JXeA/ra9F8K2jyO4Bnxr0dRPsy7JaMqSlxqTjUGHe1Z+Fm5HMjCWqkIYvp/oCbdJEivZ5pLvAtK0csQ==" crossorigin="anonymous"></script>