Return to Snippet

Revision: 12261
at March 7, 2009 15:12 by Sephr


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

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


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

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


Initial Code
if (typeof fn == "undefined" ) var fn = {};
fn.call = function call(fn /*, [thisp, [arg1, ..., argN]]*/) 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
*Requires JavaScript 1.8*

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) (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 [JS 1.8]

Initial Tags


Initial Language
JavaScript