Arguments (Array-like Objects)
arguments
Object is a local variable that is available in all (non-arrow) functions
Has four properties (only three per spec----caller)
- Arguments.callee---Point to the currently executing function
- Agruments.caller----point to the function that called the current function (removed)
- Arguments.length---points to the number of arguments passed to the current function
- Arguments.arguments---Returns a new array iterator object that contains the value of each index in the parameter
arguments
objects can be used in conjunction with remaining parameters , default parameters , and deconstructed assignment parameters
functionfunc (.... args) {returnArgs}func ()//[A]/* --------------------------------------------------------- */functionfunc (a) {arguments[0] = 99;//updated Arguments[0] also updated aConsole.log (a);} Func (10);// About"Use Strict"func (10);//Ten/* --------------------------------------------------------- */functionfunc (a) {a= 99;//updated a likewise updated arguments[0]Console.log (arguments[0]);} Func (10);// About"Use Strict"func (10);//Ten/* --------------------------------------------------------- */functionFunc (A = 55) {arguments[0] = 99;//updated arguments[0] does not update aConsole.log (a);} Func (10);//Ten/* --------------------------------------------------------- */functionFunc (A = 55) {a= 99;//updated A does not update arguments[0]Console.log (arguments[0]);} Func (10);//TenSummary: Not in strict mode, arguments[0] changes will change the corresponding parameters, modify the parameters, arguments[0] will be modified accordingly and will not be modified in strict mode
Array
has three properties, 3 methods, n instance methods (forgive me for not counting)
An array is a class list object whose prototype provides an associated operation for traversing and modifying elements
The difference between an array and a arguments
Array is a constructor typeof Array (function)
Arguments is a local variable that can be used in all (non-arrow) functions only in the function
How to convert arguments to array ( excerpt from Hanzichi teacher )
functionfn () {//uncaught TypeError:arguments.push is not a function //Arguments.push (4); vararr = []; for(vari = 0, len = arguments.length; i < Len; i++) Arr[i]=Arguments[i]; Arr.push (4);//[1, 2, 3, 4]}FN (1, 2, 3);/* ------------------------------------------ */functionfn () {vararr =Array.prototype.slice.call (arguments); Arr.push (4);//arr, [1, 2, 3, 4]}FN (1, 2, 3), or you can save a few bytes with [] instead of array.prototype. functionfn () {vararr =[].slice.call (arguments); Arr.push (4);//arr, [1, 2, 3, 4]}FN (1, 2, 3);/* ------------------------------------------ *///ES6 Syntaxvarstr = "HelloWorld";vararr =Array.from (str);//["H", "E", "L", "L", "O", "w", "O", "R", "L", "D"]/* ------------------------------------------ *///Compatible IE methodsfunctionNodelisttoarray (nodes) {vararr, length; Try { //works in every browser except IEarr =[].slice.call (nodes); returnarr; } Catch(err) {//slower, but works in IEarr = []; Length=nodes.length; for(vari = 0; i < length; i++) {Arr.push (nodes[i]); } returnarr; }}
Data reference: MDN Arguments mdn Array Hanzichi Teacher's opinion
Arrays and arguments