Skip to content

Instantly share code, notes, and snippets.

@Breefield
Created August 20, 2012 06:24
Show Gist options
  • Save Breefield/3401553 to your computer and use it in GitHub Desktop.
Save Breefield/3401553 to your computer and use it in GitHub Desktop.
Call/Apply
// You wrote this line
Function.prototype.call(current[ev], this, arguments);
// This line does what I want, calling the method on the current tool and passing the arguments.
current[ev].apply(this, arguments);
// Not sure what you were aiming for with the other line?
@junosuarez
Copy link

I meant to use apply. Invoking the function via Function.prototype helps guard against Cannot call method 'apply' of undefined errors if the object assigned to current doesn't have the event in question for whatever reason.

> current = {}
Object
> current['bogus'].apply(this, [])
TypeError: Cannot call method 'apply' of undefined
> Function.prototype.apply(current['bogus'], this, [])
undefined

@junosuarez
Copy link

Caveat that I ran into today: using call/apply on Function.prototype invokes it as expected, but returns undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment