-
Notifications
You must be signed in to change notification settings - Fork 22.5k
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
Include the this
object in method syntax
#17981
Comments
I would say, just this (without a mention in parameters)
I think pretty everybody understands that the |
And this as a "bad-example" (
|
The more important part is that Array.prototype.map.call({ 0: 1, 1: 2, 2: 3, length: 3 }, (e) => console.log(e));
({ map: Array.prototype.map, 0: 1, 1: 2, 2: 3, length: 3 }).map((e) => console.log(e)); So to some extent, the As for |
I see: what is the condition on a |
Many methods in ECMA262 have an editorial note saying it's "intentionally generic". Such methods usually use duck-typing and only require the object to behave to the extent needed by the method. For example, (For some supplementary material, last month I did an experiment which is published here: https://github.com/Josh-Cena/js-stalker) |
Thinking aloud. We could keep the
This would also work for other intentionally generic features. We could link intentionally generic to a new glossary entry about duck typing with some more generic explanation. |
That sounds good as well. |
Isn't the |
The |
Thanks. I don't understand why the this belonging to the array matters, so I'll exit the discussion. |
Would this also apply to Web APIs?
or if not, how should this be restricted? And might introduction of explicit
* Plenty of methods access the |
Yes, that's true. I'll probably start with JS APIs, but it equally applies to web APIs as well. |
Well, it's just that it would really become bloat, because I'm pretty sure that all Web API methods that have a |
We can probably ignore that in "Parameters" in this case, but the fact that it throws makes it more essential to be demonstrated in the "Syntax" part. |
I'm not convinced to add In 99% of the cases, this is obvious and only makes the documentation longer and therefore more complex and harder to understand. As this is a major change to the structure of the pages, If you want to do this, you should start a discussion in mdn-community/discussion and reach a consensus there. Keep in mind that it is the holiday season and several maintainers are out until mid-August. I wouldn't claim a consensus before they come back. For the moment, the only thing I agree we should do is to add a note for cases (in JS only) where methods are intentionally generic and can apply to other objects than the one they are defined on. Once the discussion has started, we should close this issue, so we have a discussion at one place. |
This issue is mostly about the "Syntax" section, with "Parameters" as a favorable though not necessary enhancement. I'm okay to add it as a note/example everywhere, although I do believe that's even more space taken.
Thought about it but it looks more like a bug because our current syntax is literally incorrect 😄 I would start a discussion about how we should formally document the semantics of |
I've started a discussion at mdn/mdn-community#148. Until we reach consensus there, we'll keep this closed. |
Thank you! |
MDN URL
Taking https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map as example
What specific section or headline is this issue about?
Syntax
What information was incorrect, unhelpful, or incomplete?
The "syntax" is incomplete:
map((element) => { /* ... */ })
does not highlight the fact that the method needs to have a correctthis
bound.For example, if you do:
It throws:
TypeError: Array.prototype.map called on null or undefined
What did you expect to see?
For all methods that require the correct value of
this
, we should treatthis
as part of the parameters (it actually is, in many interpretations of ECMAScript semantics), and include it in both the "Syntax" and "Parameters" section.For example, here we can write:
And then in "Parameters", add:
This is especially important for array and string methods, because they are intentionally designed to be generic, i.e. they don't require
this
to be an array/string exotic object, but can be anything that "behaves" like an array/string.Do you have any supporting links, references, or citations?
https://tc39.es/ecma262/#sec-array.prototype.map:
Do you have anything more you want to share?
No response
The text was updated successfully, but these errors were encountered: