-
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
Add section to all array methods about genericness #21137
Conversation
190f00b
to
8784601
Compare
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.
Very nice.
files/en-us/web/javascript/reference/global_objects/array/findlast/index.md
Outdated
Show resolved
Hide resolved
files/en-us/web/javascript/reference/global_objects/array/findlastindex/index.md
Outdated
Show resolved
Hide resolved
files/en-us/web/javascript/reference/global_objects/array/flat/index.md
Outdated
Show resolved
Hide resolved
files/en-us/web/javascript/reference/global_objects/array/from/index.md
Outdated
Show resolved
Hide resolved
files/en-us/web/javascript/reference/global_objects/array/reverse/index.md
Outdated
Show resolved
Hide resolved
The following code creates the `myFish` array-like object containing four | ||
elements and a length parameter, then removes its last element and decrements the length | ||
parameter. | ||
The `pop()` method reads the `length` property of `this`. If the length is 0, `length` is set to `0` again (whereas it may be negative or `undefined` before). Otherwise, the property at `length - 1` is returned and [deleted](/en-US/docs/Web/JavaScript/Reference/Operators/delete). |
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.
Sorry I don't understand this.
If the length is 0,
length
is set to0
again (whereas it may be negative orundefined
before).
If length is 0, how could it have been "negative or undefined
before"? Before what?
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 struggle to explain this... This is explained in array/index.md
:
The
length
property is converted to a number, truncated to an integer, and then clamped to the range between 0 and 253 - 1.NaN
becomes0
, so even whenlength
is not present or isundefined
, it behaves as if it has value0
.
Maybe I should say "if the normalized length is 0"?
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.
Proposed an improvement in 90206f2
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 think this is better but I don't love "normalized length" on its own, because "normalize" to me can mean all kinds of things.
I think it would be better to do something like this if you had a #### Normalizing the length property
in https://pr21137.content.dev.mdn.mozit.cloud/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#generic_array_methods, just before the paragraph about "The length property is converted to a number...".
You would also need something like #### Array-like objects
before the paragraph starting "The term array-like object".
Then you can link "[normalized length]" to that "Normalizing the length property" fragment, and the connection should be really direct for people.
But this is just a suggestion, if you don't like it I'm happy to merge this PR as it is.
@@ -78,6 +76,28 @@ while (typeof (i = names.shift()) !== 'undefined') { | |||
// Andrew, Edward, Paul, Chris, John | |||
``` | |||
|
|||
### Calling shift() on non-array objects | |||
|
|||
The `shift()` method reads the `length` property of `this`. If the length is 0, `length` is set to `0` again (whereas it may be negative or `undefined` before). Otherwise, the property at `0` is returned, and the rest of the properties are shifted left by one. The `length` property is decremented by one. |
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.
Again, I don't understand this.
If the length is 0,
length
is set to0
again (whereas it may be negative orundefined
before).
If length is 0, how could it have been "negative or undefined
before"? Before what?
files/en-us/web/javascript/reference/global_objects/array/slice/index.md
Outdated
Show resolved
Hide resolved
|
||
The usual practice is to access {{jsxref("Array/length", "length")}} and calculate the index from that — for example, `array[array.length - 1]`. The `at()` method allows relative indexing so this can be shortened to `array.at(-1)`. More formally, when `index < 0`, `index + array.length` is accessed. | ||
|
||
The `at()` method is [generic](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#generic_array_methods). It only expects the `this` value to have a `length` property and integer-keyed properties. |
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.
This seems repetitive and redundant, given "Array methods are always generic" in https://pr21137.content.dev.mdn.mozit.cloud/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array.
I think personally we would do would be better to omit it, except when there are specific things to note about it. But I won't insist on this if you don't agree.
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 think it's better if we can put it on every page—maybe one day it will no longer be the case that all array methods are generic (though I doubt it). Not many people read the Array landing page either, and it's better if we can make every page self-contained, or at least include a cross-linking (which is the main purpose here).
Co-authored-by: wbamberg <will@bootbonnet.ca>
Thanks for being so careful with the examples @wbamberg You can tell I was not feeling the greatest when writing this stuff. |
Added two headings; let's see if this is better. |
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.
This looks great @Josh-Cena , thank you for your patience with my pedantry :).
Thanks for all the review! 😉 |
Description
Like #20568, this time for generic array methods.
Motivation
We mention this property on some pages but not all of them, and quite inconsistently. This may be part of mdn/mdn-community#148
Additional details
Related issues and pull requests