Skip to content Skip to sidebar Skip to footer

Cannot Read Property 'apply ' Of Undefined

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) {

Solution 1:

If you invoke the function function(original) {} you return a the function function(x, y) {} and you create a closure over the parameter original. So the code inside of the function(x, y) {} has allways access to what ever you passed to function(original) {}

With:

o.method = (function(original) {
  returnfunction(x, y) {
    // ... 
  }
})(o.method);

You assign that returned function(x, y) {} function having a closure over original to o.method passing the original o.method as argument.

If you now write o.method(1,2) you call that returned function(x, y) {}.

If you called that function(original) {} with no argument (function(original) {})() then original is undefined and original.apply would result in your posted error.

Solution 2:

With some change your code is correct:

var o = newObject();
o.method2 = function(x,y){return x+y};

o.method = (function (original){
    returnfunction(x,y){
            var result = original.call(this, x,y);
        return result;
    }
})(o.method2);

Also for test and run:

o.method(2,3)

Result is 5

Also with Apply:

var o = newObject();
o.method2 = function(x,y){return x+y};

o.method = (function (original){
    returnfunction(x,y){
            var result = original.apply(this, [x,y]);
        return result;
    }
})(o.method2);

Then test:

o.method(2,3)

Result is 5

For more information about the usage of call() and apply() you can read this article: http://adripofjavascript.com/blog/drips/invoking-javascript-functions-with-call-and-apply.html

Post a Comment for "Cannot Read Property 'apply ' Of Undefined"