hi Man, i don’t have strong opinion on Array.prototype.remove, but i have
strong opinion against your use-case to subclass/extend Array. from a
product-development perspective, you're creating unnecessary
integration-headaches by having the client <-> server system pass around
<MyArray> instances instead of plain JSON arrays.
i remain unconvinced of any real-world benefit to subclassing Array, that
justifies the cost-added to already painful task of debugging end-to-end
communication-issues between client <-> server. your web-project will have a
less-painful integration/qa process, if you stick with plain JSON arrays
employing static-functions instead:
```javascript
/*jslint devel*/
(function () {
"use strict";
var myArray1;
function arrayRemoveItem(array, item) {
/**
* This static-function will:
* Remove the first occurrence of [item] from this array.
* Return `true` if [item] was in this array, `false` otherwise.
*/
var i = array.indexOf(item);
if (i >= 0) {
array.splice(i, 1);
return true;
}
return false;
}
myArray1 = [1, 2, 3, 4]; // [1, 2, 3, 4]
console.log(arrayRemoveItem(myArray1, 2)); // true
console.log(myArray1); // [1, 3, 4]
}());
```
kai zhu
[email protected]
> On 10 Oct 2018, at 3:01 PM, Man Hoang <[email protected]> wrote:
>
> The problem with `Set` is that its `add` method returns `this` instead of
> `boolean`. If `Set.prototype.add` returned `boolean`, I would have used `Set`.
>
> That’s why in the `select` method of my sample code, I use a custom defined
> method named `pushIfAbsent`. The actual type of `_values` is not `Array` but
> a subclass of `Array`.
> ``` js
> export class MyArray<E> extends Array<E> {
> /**
> * Adds [item] to the end of this array if it's not already in this array.
> *
> * Returns `true` is [item] was added, `false` otherwise.
> */
> pushIfAbsent(item: E): boolean {
> if (!this.includes(item)) {
> this.push(item);
> return true;
> }
> return false;
> }
>
> /**
> * Removes the first occurrence of [item] from this array.
> *
> * Returns `true` if [item] was in this array, `false` otherwise.
> */
> remove(item: E): boolean {
> const i = this.indexOf(item);
> if (i >= 0) {
> this.splice(i, 1);
> return true;
> }
> return false;
> }
> }
> ```
> _______________________________________________
> es-discuss mailing list
> [email protected] <mailto:[email protected]>
> https://mail.mozilla.org/listinfo/es-discuss
> <https://mail.mozilla.org/listinfo/es-discuss>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss