0

So, I'm trying to override a function but include it's original methods with the jQuery.extend() method.

    var origFunction = $.fn.pluginFunction;
    $.fn.extend({
        pluginFunction: function() {
               // `origFunction` is available via Closure, 
               // now how can I declare the $.extended function here
               // to preserve the original methods and then override
               // only the following object?

                   myObject = {
                        'key1' : 'val1',
                        'key2' : 'val2',
                   }
        }
    });
Brian
  • 3,920
  • 12
  • 55
  • 100

3 Answers3

2

Use apply() to call the original.

origFunction.apply(this,arguments);

but if myObject is a local variable inside of origFunction, it is not going to make a difference.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • This doesn't work because executing `origFunction.apply(this,arguments)` executes the function prematurely . I'm trying to debug it, and add a callback so that I can ensure the function is properly extended before executing it but the `origFunction()` apply that you suggested is actually the source of my problem - walking through it in debugger, it is at this line where the plugin executes prior to being extended. – Brian Oct 04 '12 at 20:15
  • How does it execute it prematurely? Not getting what you mean. – epascarello Oct 04 '12 at 20:35
2

Modified code: call origFunction using apply. You change myObject only if it is not a private variable of method(it should be global accessible from overriding method).

var origFunction = $.fn.pluginFunction;
    $.fn.extend({
        pluginFunction: function() {
               origFunction.apply(this, arguments); // 
               // `origFunction` is available via Closure, 
               // now how can I declare the $.extended function here
               // to preserve the original methods and then override
               // only the following object?

                   myObject = {
                        'key1' : 'val1',
                        'key2' : 'val2',
                   }
        }
    });
Anoop
  • 23,044
  • 10
  • 62
  • 76
  • This doesn't work because executing `origFunction.apply(this,arguments)` executes the function prematurely . I'm trying to debug it, and add a callback so that I can ensure the function is properly extended before executing it but the `origFunction()` apply that you suggested is actually the source of my problem - walking through it in debugger, it is at this line where the plugin executes prior to being extended. – Brian Oct 04 '12 at 20:17
0

User apply()

apply calls a function with a set of arguments. It's not part of jQuery, it's part of core Javascript. However, there is mention of it in the jQuery docs:

http://docs.jquery.com/Types#Context.2C_Call_and_Apply

Syntax:

somefunction.apply(thisobj, argsarray)

The above calls the function somefunction, setting this to thisobj within the function's scope, and passing in the arguments from argsarray as the arguments to the function.

Anshu
  • 7,783
  • 5
  • 31
  • 41