Return to Snippet

Revision: 12265
at March 7, 2009 13:33 by Sephr


Updated Code
if (typeof fn == "undefined" ) var fn = {};
fn.call = function call(fn /*, [thisp, [arg1, ..., argN]]*/) { return Function.prototype.call.apply(fn, Array.prototype.slice.call(arguments).splice(1, arguments.length)) };
fn.apply = function apply(fn, thisp, args) {
  if ( args ) args.splice(0, 0, thisp);
  return Function.prototype.call.apply(fn, args)
};

Revision: 12264
at March 7, 2009 13:25 by Sephr


Updated Code
if (typeof fn == "undefined" ) var fn = {};
fn.call = function call(fn /*, [thisp, [arg1, ..., argN]]*/) { return Function.prototype.call.apply(fn, Array.prototype.slice.call(arguments).splice(1, arguments.length)) };
fn.apply = function apply(fn, thisp, args) {
  args.splice(0, 0, thisp);
  return Function.prototype.call.apply(fn, args)
};

Revision: 12263
at March 7, 2009 13:18 by Sephr


Updated Code
if (typeof fn == "undefined" ) var fn = {};
fn.call = function call(fn /*, [thisp, [arg1, ..., argN]]*/) { return Function.prototype.call.apply(fn, Array.prototype.slice.call(arguments).splice(1)) };
fn.apply = function apply(fn, thisp, args) {
  args.splice(0, 0, thisp);
  return Function.prototype.call.apply(fn, args)
};

Revision: 12262
at March 7, 2009 13:15 by Sephr


Initial Code
if (typeof fn == "undefined" ) var fn = {};
fn.call = function call(fn /*, [thisp, [arg1, ..., argN]]*/) { return Function.prototype.call.apply(fn, Array.prototype.slice.call(arguments).splice(1)) }
fn.apply = function apply(fn, thisp, args) {
  args.splice(0, 0, thisp);
  return Function.prototype.call.apply(fn, args)
};

Initial URL

                                

Initial Description
fn.call and fn.apply are alternative methods to access function.call and function.apply and I don't see fn.call and fn.apply having any use but I made them for the heck of it.

Syntax:

`fn.call(function, [thisObject, [argument1, ..., argumentN]])` is equivalent to `function.call(thisObject, argument1, ..., argumentN)`

`fn.apply(function, [thisObject, [argumentsArray]])` is equivalent to `function.apply(thisObject, argumentsArray)`

Examples:

    function foo(a, b) { return (this*a)-b }
    fn.call(foo, 5, 6, 7) == 23
    foo.call(5, 6, 7) == 23
    fn.apply(foo, 5, [6, 7]) == 23
    foo.apply(5, [6, 7]) == 23

Initial Title
fn.call and fn.apply

Initial Tags

                                

Initial Language
JavaScript