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 2 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 @@ -24,41 +24,63 @@ property of `arguments` is callee. This always refers to the function
currently being executed. 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.callee === myfunc;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
copy the values into a true array first. Since a normal `for` loop
works, this is pretty easy, or if you are working with ES6 and above `Array.from()` makes this transfer even easier.

var args = [];
for(var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
```js
// for ES5 and below
const args = [];
for(let i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}

// for ES6 and above
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should push this as the default for current Node.js versions (and only mention the pre-ES6 variants briefly). What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @fhemberger for the advice will update both the sections :)

const args = Array.from(arguments);
```

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 funtions` were added in the ECMAScript 2015 (ES6) specification as 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 casese 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);
```