I am trying to understand the .apply-method. So, I created the following code:
var o = new Object();
o.method = function(x, y) {
return x + y
};
o.method = (function(original) {
return function(x, y) {
var a = 'A';
var b = 'B';
var result = original.apply(this, [a, b]);
return result;
}
})(o.method);
console.log(o.method(1, 2));
I thought that the last open and close parentheses could be empty and so the method o.method is invoked automatically. But with empty parentheses
o.method = (function(original) {
return function(x, y) {
var a = 'A';
var b = 'B';
var result = original.apply(this, [a, b]);
return result;
}
})();
o.method(1, 2);
I get the following error: Cannot read property 'apply' of undefined
Can someone please explain me why?
Kind regards Henning