Last active
August 29, 2015 14:07
-
-
Save lizlongnc/cf4c670543dd31b04c76 to your computer and use it in GitHub Desktop.
JS built-in pseudo parameter (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
// All JS functions receive 2 pseudo parameters - arguments and this. | |
// The following uses JS built-in pseudo parameter (arguments). | |
// arguments is an array-like thing that contains all the arguments passed to the function | |
// if an argument changes, arguments also changes which can sometimes slow things down | |
// can access arguments.length | |
// and can use arguments to manipulate values passed to the function | |
function sum() { | |
'use strict'; | |
var i = 0, total = 0; | |
for (; i < arguments.length; i++) { | |
total += arguments[i]; | |
} | |
return total; | |
} | |
sum(1,1,1,1); // 4 | |
var mySum = sum(1,2,3,4,5); // 15 | |
console.log(sum(2, 4)); // 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment