Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #30278
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: duplicator an array |
| Date | 2016-04-14 22:09 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <1593194.mKTEO0Afz8@PointedEars.de> (permalink) |
| References | <ea578169-9d58-4249-91b4-e0cbad5a6fd8@googlegroups.com> <nen4e4$ggn$1@dont-email.me> <neokoi$6k3$1@solani.org> |
Christoph M. Becker wrote:
> On 14.04.2016 at 06:00, Aleksandro wrote:
>> On 13/04/16 22:27, JRough wrote:
>>> Okay, supposing the above works then I thought I would make an anonymous
>>> function in the duplicator method. This would make it work the way the
>>> problem was first outlined above. The only problem is how do I capture
>>> the value of the two arrays?
>>>
>>>
>>> I guess you would do something like Array.prototype.duplicator =
>>> (function (){
>>> return this.concat(arr.slice(0));
>>> })()
>>
>> Try:
>>
>> Array.prototype.duplicate = function ()
>> {
>> this.push.apply(this, this.slice(0))
>> }
>
>
> The usual caveats wrt. augmenting built-in objects apply, though, see
> e.g.
> <http://peter.michaux.ca/articles/javascript-the-good-parts-built-in-object-augmentation-and-namespacing>.
It should be noted that the referred article is *old*; it is dated
2008-06-18.
We now have String.prototype.trim(), and we have .toJSON() as a hook for
JSON.stringify() in the language standard (ES 2015, § 24.3.2, step 12), and
it is implemented. So it would seem that either Douglas Crockford was
prophetic or, more likely, that his were good examples that later made it
into the standard. JFTR, String.prototype.trim() has been in the FAQ for
quite a while (currently in § 8.1), and I have written toJSON() methods.
Also, one important reason for not augmenting the object referred to by
Array.prototype, and by extension any prototype, has disappeared: we have
standard property descriptors since ECMAScript Edition 5/5.1
(2009-12/2011-06) so that user-defined properties need no longer be
enumerable and need not mess up for-in iteration (useful for sparse arrays
if order is irrelevant):
try
{
Object.defineProperty(Array.prototype, "duplicate", {
value: function () {
return this.concat(this.slice());
},
/* optional; the default for each attribute is “false” */
configurable: true,
enumerable: false,
writable: true
});
}
catch
{
console.warn("Array.prototype.duplicate cannot be (re)configured.");
}
In this case, however, there is little benefit of calling the method instead
of inlining it with a.concat(a.slice()) [the 0 argument is optional as that
is the implicit default (ES 2015, § 22.1.3.22, step 5)].
Also note that a.duplicate() as defined above does nothing of consequence;
in order for it to duplicate the array items in the desired way it would
have to define array items explicitly or its return value would need to be
assigned to something.
In any case, it is possible to safely augment built-in prototype objects if
you limit the scope of the change:
(function () {
var arrayDuplicate = Array.prototype.duplicate;
Array.prototype.duplicate = function () {
return this.concat(this.slice());
};
/* do something */
Array.prototype.duplicate = arrayDuplicate;
}());
This is safe only if there are no concurrent threads in the same global
context as all share the same “Array” object and one of them might rely on a
different implementation. DNS-based namespaces, as suggested in the
article, might be a solution to that; but all parties involved have to agree
to that for it to work reliably.
Otherwise the only safe approach is to augment the instance instead of its
prototype:
(function () {
var a = [1, 2, 3];
a.duplicate = function () {
return this.concat(this.slice());
};
// …
/* [1, 2, 3, 1, 2, 3] */
a = a.duplicate();
// …
}());
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
duplicator an array JRough <janis.rough@gmail.com> - 2016-04-13 18:27 -0700
Re: duplicator an array Aleksandro <aleksandro@gmx.com> - 2016-04-14 01:00 -0300
Re: duplicator an array JRough <janis.rough@gmail.com> - 2016-04-14 09:53 -0700
Re: duplicator an array "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-04-14 19:41 +0200
Re: duplicator an array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-14 22:09 +0200
Re: duplicator an array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-14 22:23 +0200
Re: duplicator an array Stefan Weiss <krewecherl@gmail.com> - 2016-04-16 14:27 +0200
Re: duplicator an array Joao Rodrigues <jr@none.com> - 2016-04-16 12:01 -0300
Re: duplicator an array Joao Rodrigues <jr@none.com> - 2016-04-16 13:43 -0300
Re: duplicator an array Aleksandro <aleksandro@gmx.com> - 2016-04-16 14:13 -0300
Re: duplicator an array bharat.bandgar009@gmail.com - 2017-05-03 01:33 -0700
csiph-web