Prototype.call() vs Prototype.apply()

This is going to be a relatively short post.  We have been doing a lot of [x] vs [y] vs [z] posts lately and this one is no different.  Today, I figured that we would look at Prototype.call() vs Prototype.apply().  Both apply() and call() are used to invoke functions.  The difference is that call uses a comma separated list of arguments and apply uses an array of arguments.

Prototype.call() example:

See the Pen
prototype.call()
by Joe Steinbring (@steinbring)
on CodePen.

Prototype.apply() example:

See the Pen
prototype.apply()
by Joe Steinbring (@steinbring)
on CodePen.

For the sake of clarity, both examples do the same thing beyond the one difference.  They both have teachers, students, and persons but where one uses Person.apply(this, [fname, lname]); the other one uses Person.call(this, fname, lname);.

Have a question, comment, etc?  Feel free to drop a comment, below.

 

[ Cover photo by Alexander Andrews on Unsplash ]

Leave a Reply

Your email address will not be published. Required fields are marked *