Skip to content

Conversation

miorel
Copy link
Contributor

@miorel miorel commented Aug 31, 2024

No description provided.

Copy link
Contributor

@elimanzo elimanzo left a comment

Choose a reason for hiding this comment

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

I really loved the solution explanation, and the introduction to rest parameters, const + let vs var, and how sparse arrays works. 💯


Fun fact, `arguments` don't work the same way in arrow functions. [The documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) phrases it:

> Arrow functions don't have their own bindings to `this`, `arguments`, or `super`
Copy link
Contributor

Choose a reason for hiding this comment

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

ah thanks for the reminder, I was wondering why using arguments wasn't working with arrow functions

*/
```

However, in TypeScript, omitting the rest parameter will result in a type error in the code LeetCode uses to invoke our function! A common practice is to prefix the names of unused variables with an underscore:
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice to know!

Comment on lines +302 to +313
If the arrow function is defined in the global scope, we'll get a `ReferenceError` that `arguments` is not defined. If the arrow function is defined within the body of a non-arrow function, we'll be accessing the `arguments` of the enclosing function!

You can test with the following code:

```js
function enclosing() {
const arrow = () => arguments.length;
return arrow();
}

console.log(enclosing("a", "b", "c")); // prints 3
console.log(enclosing("a", "b")); // prints 2
Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome!

@miorel miorel merged commit 2352116 into main Aug 31, 2024
6 checks passed
@miorel miorel deleted the arg-length-documentation branch August 31, 2024 23:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants