Home  >  Article  >  Web Front-end  >  Introduction to the usage of the Arguments object in javaScript

Introduction to the usage of the Arguments object in javaScript

不言
不言forward
2019-03-20 10:36:552178browse

This article brings you an introduction to the usage of the Arguments object in javaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Figure out what arguments are

“arguments is an array-like object corresponding to the parameters passed to the function.
arguments object is Local variables available in all (non-arrow) functions. You can reference a function's arguments within a function using the arguments object. This object contains each argument passed to the function, with the first argument at index 0."

First of all, it is an array-like object. The result of typeof arguments is undoubtedly "object". Note that the result is of string type. Next, Object.prototype.toString.call(arguments) is called, and the result is "[object Arguments]" that has never been seen before.

2. Convert to array

1.Array’s silce method

Array.prototype.slice.call(arguments)

2.Array.from

let re = Array.from(arguments)

3. Expansion operator

let re = [...arguments]

3. From arguments to class array

The class array must have a length attribute and an index attribute. The following is explained with the code:

let obj = {

            "0": 'a',

            "1": 'b',

            "2": 'c',

            length: 3,

            "push": Array.prototype.push,

            "splice": Array.prototype.splice

        }

obj.push('d')

console.log(obj)

The result is:

Introduction to the usage of the Arguments object in javaScript

The actual execution process is equivalent to:

obj[obj.length] = 'd';
obj.length++;

4. Written test questions

var length = 10;
function fn(){
    console.log(this.length)
}
var obj = {
    length: 5,
    getF: function(fn) {
        fn();
        arguments[0]();
    }
}
obj.getF(fn);

What we examined were the arguments and this pointing question. I answered 5 1, but the actual result was 10 1. This is my first time writing an article, I hope it will be helpful to you.

This article is all over here. For more other exciting content, you can pay attention to the JavaScript Tutorial Video column on the PHP Chinese website!

The above is the detailed content of Introduction to the usage of the Arguments object in javaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete