I'm recently reading the "Effective javascript". In the item "Use call methods with a custom receiver". The author provides an example of adding the contents of one table to another using this method :
var table = {
entries: [],
addEntry: function(key, value) {
this.entries.push({key: key, value: value});
},
forEach: function(f, thisArg) {
var entries = this.entries;
for (var i = 0, n = entries.length; i < n; i++)
{
var entry = entries[i];
f.call(thisArg, entry.key, entry.value, i);
}
}
};
Now if I test this method writing things like:
table2 = table;
table1 = table;
table1.entries = [1, 3];
table1.forEach(table1.addEntry, table2);
console.log(table2.entries);
I get the contents of table2 entries as [1, 3, Object, Object]. Which confuses me. Why do we get those two additionnal objects. Probably, I didn't get the call method properly. Can someone explain to me what is happening ?