0

I don't understand why arguments is a Array-like in JS function?

how can i find the length property in arguments?

why Object.keys(arguments) cann't output length property?

2

3 Answers 3

1

Just try and you get it:

$ js 
> !function(){console.log(arguments.length);}(1,2,3)
3

Also:
>!function(){ console.log(Object.keys(arguments).length); }(1,2,3)
3
Sign up to request clarification or add additional context in comments.

Comments

0

arguments is array-like in JavaScript for a few reasons, such as allowing the flexibility you can have without type safety, and being able to make "overloads" - not real overloads, but you can make the same function serve as a getter and a setter for example, since you wouldn't be able to have an overload in js anyways.

take the following for example:

var theName;

function name() {
    if (arguments.length > 0) {
        theName = arguments[0];
    } else {
        return theName;
    }
};

as you can see, the same function can then serve a couple different functions in your code (of course you want to use this sparingly - but take for example the $(); aka jQuery() function in jQuery - it does quite a few different things see more on it here. Same thing for the internet staple $.ajax() see more on that here.

if it wasn't an array-like object you would get an error from passing more or less arguments than required by the function signature. that doesn't mean you can't throw an error if an argument is missing in your function - but it gives you the flexibility to have optional params, or pass an array or a string in the same function to add or augment the purpose or the result of the code. - it allows for many of the things that make JavaScript so flexible(such as Function.prototype.apply()).

In short, the reason it is array like is fundamental to the way JavaScript works and why it is so flexible.

Also, how the argument object works is like an array (so access by index, i.e. arguments[0]) and it has a length property.

EDIT:

I saw you also asked why Object.keys(arguments) does not work - this is because arguments is a special object that doesn't work that way the length property is different on the arguments object than on a standard array or JS object. - you will either need to copy the arguments into an array, access it by index, or use one of the methods listed here.

Comments

0

I don't understand why arguments is a Array-like in JS function?

Because the specs say so. From this spec ref here:

When control enters an execution context for function code, an arguments object is created unless (as specified in 10.5) the identifier arguments occurs as an Identifier in the function’s FormalParameterList or occurs as the Identifier of a VariableDeclaration or FunctionDeclaration contained in the function code.

You can check that it is an object (array like) and not actually an array by testing it out like this:

function test(param1, param2) {
    document.getElementById("result").innerText += arguments instanceof Array;
    document.getElementById("result").innerText += arguments instanceof Object;
    document.getElementById("result").innerText += arguments.length;
}

test("str1", "str2");
<p id="result"></p>

how can i find the length property in arguments?

Use the length as in arguments.length.

.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.