-
Notifications
You must be signed in to change notification settings - Fork 13
Write-up for LeetCode problem 2703. Return Length of Arguments Passed #399
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
Conversation
There was a problem hiding this 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` |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice to know!
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
No description provided.