Posted By

Sephr on 03/07/09


Tagged

call js18 apply


Versions (?)

fn.call and fn.apply [JS 1.8]


 / Published in: JavaScript
 

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
  1. if (typeof fn == "undefined" ) var fn = {};
  2. fn.call = function call(fn /*, [thisp, [arg1, ..., argN]]*/) Function.prototype.call.apply(fn, Array.prototype.slice.call(arguments).splice(1));
  3. fn.apply = function apply(fn, thisp, args) {
  4. if ( args ) args.splice(0, 0, thisp);
  5. return Function.prototype.call.apply(fn, args)
  6. };

Report this snippet  

You need to login to post a comment.