Arrays and arguments

Source: Internet
Author: User

Arguments (Array-like Objects)

  argumentsObject 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

argumentsobjects 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.