Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update what-is-the-arguments-object.md #2085

Merged
merged 3 commits into from
Feb 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,64 @@ access them.
The `arguments` object is an array-like object. It has a length
property that corresponds to the number of arguments passed into the
function. You can access these values by indexing into the array,
e.g. `arguments[0]` is the first argument. The only other standard
property of `arguments` is callee. This always refers to the function
currently being executed. Here's an example that illustrates the
e.g. `arguments[0]` is the first argument. The only other
property of `arguments` is callee, which ES5 forbids to use in `strict mode` more about it could be found [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee). Here's an example that illustrates the
properties of `arguments`.

var myfunc = function(one) {
arguments.callee === myfunc;
arguments[0] === one;
arguments[1] === 2;
arguments.length === 3;
}
```js
const myfunc = function(one) {
arguments[0] === one;
arguments[1] === 2;
arguments.length === 3;
}

myfunc(1, 2, 3);
myfunc(1, 2, 3);
```

This construct is very useful and gives javascript functions a lot of
flexibility. But there is an important gotcha. The `arguments` object
behaves like an array, but it is not an actual array. It does not have
Array in its prototype chain and it does not respond to any array
methods, e.g. `arguments.sort()` raises a TypeError. Instead you need to
copy the values into a true array first. Since a normal for loop
works, this is pretty easy.
methods, e.g. `arguments.sort()` raises a TypeError. Instead, you need to
copy the values into a true array first. With the advent of ES6 `Array.from()` method this is quite straightforward.

var args = [];
for(var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
```js
const myfunc = function(a, b, c) {
const args = Array.from(arguments);
console.log(args) // [1, 2, 3]
}

myfunc(1, 2, 3);
```
NOTE: For ES5 and below, a normal `for` loop can do the trick.

In certain cases you can still treat `arguments` as an array. You can
use `arguments` in dynamic function invocations using apply. And most
native Array methods will also accept `arguments` when dynamically
invoked using call or apply. This technique also suggests another way
to convert `arguments` into a true array using the Array#slice method.
to convert `arguments` into a true array using the `Array.slice` method.

```js
myfunc.apply(obj, arguments).

// concat arguments onto the
Array.prototype.concat.apply([1,2,3], arguments);

// turn arguments into a true array
const args = Array.prototype.slice.call(arguments);

// cut out first argument
args = Array.prototype.slice.call(arguments, 1);
```

myfunc.apply(obj, arguments).
### Arguments object in arrow function

// concat arguments onto the
Array.prototype.concat.apply([1,2,3], arguments).
The `arrow functions` were added in the ECMAScript 2015 (ES6) specification as a syntactically compact alternative to a regular function expression. A drawback to this new alternative is the lack of `arguments object` (and `this`, `super`, and `new.target` keywords). A workaround for such cases is the use of `rest parameter`. The `rest parameter` allows you to represent an indefinite number of arguments as an array. For more details read [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters).

// turn arguments into a true array
var args = Array.prototype.slice.call(arguments);
```js
const myfunc = (...args) => {
console.log('first parameter is ', args[0]);
}

// cut out first argument
args = Array.prototype.slice.call(arguments, 1);
myfunc(1, 2, 3);
```