Pivotal Labs

Main menu

Skip to primary content
Skip to secondary content
  • About
  • Case Studies
  • Team
    • Executives
    • Locations
      • San Francisco (HQ)
      • Boston
      • Boulder
      • Denver
      • London
      • Los Angeles
      • New York
  • Community
    • Blogs
    • Tech Talks
    • Events
  • Careers
    • Lifestyle
    • Principles & Practices
    • Benefits
    • FAQ
    • Apply
  • Contact
    • Press Room
    • Press Releases
    • In The News
    • Press Kit
  • All
  • Labs
  • Standup
  • Tracker

How I remember the difference between Function.call and Function.apply

Mark Rushakoff
Tuesday, September 4, 2012

In Javascript, when you use a function as a first-class-object, you can call the function directly without any special context:

 function execute(func) {
     func();
 }

where the context is the value of this in the function when it’s executed.

If you’re not invoking the function with any arguments, it doesn’t really matter whether you use .apply or .call:

function execute(func, context) {
    // these two statements have the same effect
    func.call(context);
    func.apply(context);
}

But as soon as you need to pass arguments…

 function executeWith123(func, context) {
    // these two statements also have the same effect
    func.call(context, 1, 2, 3);
    func.apply(context, [1, 2, 3]);
 }

The difference is that with call, you can write as many arguments as you need, which is handy when you know exactly which and how many arguments you’re passing. With apply, it’s easier to use a variable number of arguments.

I used to have to always look up the difference between call and apply, until I realized:

Function.apply takes an array: A is for Apply and Array.

I hope that saves you the pain of looking up the documentation or guessing and picking the wrong one next time.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter

Add New Comment Cancel reply

Your email address will not be published.

Mark Rushakoff

Mark Rushakoff
San Francisco

Recent Posts

  • git rebase vs. git merge: an agile perspective
  • An attitude shift as we approach production
  • Use fold to wrap long lines for an easier diff
Subscribe to Mark's Feed

Author Topics

agile (1)
git (2)
merge (1)
rebase (1)
attitude (1)
cloudfoundry (1)
incident-response (1)
production (1)
diff (1)
shell script (1)
text-wrapping (1)
unix (1)
gem (1)
ruby (8)
ci (1)
jenkins (1)
rspec (2)
chrome (3)
two-step-authentication (1)
headphones (1)
pair programming (1)
software engineering (1)
javascript (5)
css (2)
backbone (2)
bash (1)
html (2)
documentation (2)
rails (6)
tdd (1)
carrierwave (1)
imagemagick (1)
compass (1)
github (1)
firefox (3)
jasmine (1)
  • About
  • Case Studies
  • Team
  • Community
  • Careers
  • Contact
  • Labs
  • Events

Contact Us

contact@pivotallabs.com
+1 415-77-PIVOT
TwitterLinkedInFacebook

Pivotal Tracker

Tracker is the award-winning agile project management tool that enables real-time collaboration around a shared, prioritized backlog.
Visit pivotaltracker.com >