Last active
August 29, 2015 14:13
-
-
Save ShivrajRath/e1cb02fa573f0d5a3c4a to your computer and use it in GitHub Desktop.
Curious case of JavaScript function arguments
This file contains hidden or 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
/** | |
* Running the array reverse function on arguments object | |
*/ | |
function reverseArgs(){ | |
return Array.prototype.reverse.call(arguments); | |
} | |
reverseArgs(12,34,55); | |
// OUTPUT | |
// [55, 34, 12] | |
/** | |
* Running the array join function on arguments object | |
*/ | |
function argsJoin(){ | |
return [].join.apply(arguments,["~"]); | |
} | |
argsJoin(12,33,44); | |
// OUTPUT | |
// "12~33~44" |
This file contains hidden or 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
/** | |
* arguments object just looks like an index Array, it's not an Array object | |
*/ | |
function argsExp(){ | |
return arguments instanceof Array; | |
} | |
argsExp(); | |
//OUTPUT | |
// false |
This file contains hidden or 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
/** | |
* arguments object have an array like notation | |
*/ | |
function argsExp(){ | |
return arguments; | |
} | |
argsExp("one", "two", "three"); | |
// OUTPUT | |
// ["one", "two", "three"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment