-
-
Save eshrinivasan/30a8e387d3115c61abac68be72c686ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function takesTwoParams(a, b){ | |
var args = Array.prototype.slice.call(arguments); | |
alert(" your parameters were " + args.join(", ")); | |
} | |
Let’s take a look at that a bit more in-depth: | |
Array: This object is the original array that all other arrays inherit their properties from. | |
Array.prototype:This gives us access to all the methods properties that each array inherits | |
Array.prototype.slice: The original slice method that is given to all arrays via the prototype chain. | |
We can’t call it directly though, because when it runs internally, it looks at the this keyword, and calling it | |
here would make this point to Array, not our arguments variable. | |
Array.prototype.slice.call(): call() and apply() are prototype methods of the Function object, meaning that they can | |
be called on every function in javascript. These allow you to change what the this variable points to inside a given function. | |
And finally, you get a regular array back! This works because javascript returns a new object of type Array rather than whatever | |
you gave it. This causes a lot of headaches for a few people who are trying to make subclasses of Array, but it’s | |
very handy in our case! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment