Hello I was reading "JavaScript: the definitive guide" 6th edition and tried one of the examples at 9.1 Classes and Prototypes.
function range (from, to) {
var r = Object.create(range.methods);
r.from = from;
r.to = to;
return r;
}
range.methods = {
includes: function(x) {
return this.from <= x && x <= this.to;
},
foreach: function(f) {
for(var x = Math.ceil(this.from); x <= this.to; x++)
f(x);
},
toString: function() {
return "(" + this.from + "..." + this.to + ")";
}
};
Loading this into console throws an error
Uncaught TypeError: Illegal invocation class.js:31. range.methods.foreach class.js:31 (anonymous function)
I guess intention of foreach method is to pass a function name as an argument
var r = range(1, 3);
r.foreach(console.log);
Any ideas how to fix this error?