Replies: 2 comments
-
Recently we added page types to all WebAPI pages mdn/content#16255. As it would be apparent from title/heading whether the method is static or member, there won't be need to specify owner objects in syntax sections. And it'll keep the syntax sections concise. This hasn't been done for JS pages yet. We can skip eslinting syntax sections by marking them as
It may make the Web Docs sounding like a manual or the language specification itself. 🤔
We can add specific examples for special cases explaining the scenarios. Contributors can help with the effort if we we make |
Beta Was this translation helpful? Give feedback.
-
I think #14857 and following were a mistake.
@OnkarRuikar No, the syntax section should show the signature with an example call. And the receiver object is a very important part of it, to distinguish it from a global function. The goal is not conciseness, it's readability; and to a certain amount, copy-paste-ability. Otherwise you could argue that the method name is already in the page title as well, so why not drop that as well? Just refer to the parameters by index and drop the syntax example altogether? That is not good documentation imo. |
Beta Was this translation helpful? Give feedback.
-
JS functions have an implicit parameter called
this
. For example,map
is a function that reads every element ofthis
, and puts the mapped elements in a new array.Internally, you can see it being implemented as
If it's not apparent enough that
this
is part of the parameters, consider the TypeScript syntax, which makes it apparent thatthis
is a positional parameter:Or the fact that
Reflect.apply
, which is the reflective semantic of calling a function, takesfn
,thisArg
,args
as its three parameters.In "normal" cases, the
this
parameter is the one before the "dot". ("Normal" a.k.a minusFunction.prototype.bind
et al.) However, in the "Syntax" section, it's written as:I'd say that's factually wrong, because it's claiming the function can be called without a
this
at all! If you do that, you get aTypeError
, because thethis
passed toArray.prototype.map
is undefined:Therefore, I think it's immediately apparent that the "host" of the method should be part of the syntax, because without it it would be a
TypeError
, which is not any better than missing one required parameter.This may have other impacts as well. For example, for
Generator.prototype.return()
, we are forced to add ageneratorObject
subject, becausereturn
is a keyword and without the subject it's literally invalid syntax.Therefore, my first question is: Is it worth the effort to add a formal subject to the "Syntax" section? Do we want to have uniform naming conventions in this case (for example, camel-cased constructor name, suffixed with
Object
/Instance
)?I hope it's mostly undisputed that adding a subject to the "Syntax" section is worth it since that's what people write 99.9% of the time anyway. However, do we want to document the semantics of the
this
parameter (i.e. what does the language expect the value used asthis
to have, what happens when it's missing, what does the method do tothis
...)? If so, how?ECMAScript spec has defined a series of built-in methods that are "intentionally generic", making the
this
parameter part of the semantics and can be meaningfully customized. For example, to quoteArray.p.map
:As one can see from the code above, the algorithmic steps of
Array.p.map
, as it's defined in the spec, only does the following tothis
:this.constructor[Symbol.species]
to construct the new Array (falling back to the globalArray
so this isn't required)this.length
Therefore, it can be transferred to any array-like object:
That's such an important aspect of the language that everywhere a method is "intentionally generic", the spec leaves an editorial note. There are 110 such notes in ES2022!
As for some other methods (especially web APIs), there're strict branded checks to make sure the
this
is of the right type. For example,Map.p.get
uses the[[MapData]]
internal slot so it can't be used on non-Map
instances.I proposed in mdn/content#17981 that we should add
this
to the "Parameters" section, in light of the fact thatthis
is indeed part of the parameters. However, there're worries that it unnecessarily bloats the reference pages since most people don't call the method on non-array objects anyway. I'm therefore starting the discussion to discuss how we should document the semantics ofthis
in such a way that:this
, and the requirements for a validthis
. (Does it require an internal slot and therefore can only be called on a particular class? Does it only use certain properties?)Should we:
> **Note:**
similar to that in the spec? (Trivial effort, but without another section to describe the algorithmic steps like what I've done above, the note is basically cryptic for most readers)Prior art: on
Array.p.pop
and a few others, it's already documented that:However, these remarks are really sporadic and scattered throughout the docs, and we have far more cases where the behavior is not noted.
Beta Was this translation helpful? Give feedback.
All reactions