Home > Web Front-end > JS Tutorial > body text

Introduction to the usage of the Arguments object in javaScript

不言
Release: 2019-03-20 10:36:55
forward
2156 people have browsed it

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)
Copy after login

2.Array.from

let re = Array.from(arguments)
Copy after login

3. Expansion operator

let re = [...arguments]
Copy after login

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)
Copy after login

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++;
Copy after login

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);
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!