Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #30217
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Comparing some of the ways to create objects |
| Date | 2016-04-04 15:20 +0200 |
| Organization | albasani.net |
| Message-ID | <ndtpmv$8al$1@news.albasani.net> (permalink) |
| References | <hbc2gbpvk5nk24b0qupps6cjqn8fm3ar5e@4ax.com> <ndspod$lhh$1@dont-email.me> |
Scott Sauyet wrote:
> John Harris wrote:
>
>> It's time to compare ways of creating objects now that the full release
>> version of Firefox implements class declarations. The code below shows
>> three of the ways. They are all ones where an object is created by using
>> new and a constructor.
>
> I don't see why that is so special.
The special thing is that it provides inheritance. The new `class` syntax
doesn't add anything that wasn't possible before, the syntactic sugar just
makes it look nicer. Other types of object creation are equally valid, and
often preferable, but like the things 5 and 6 in your examples, they don't
have "is-a" semantics built in. This may or may not be a desirable feature,
depending on the circumstances (Thing6 could be adjusted to make this work).
> I will add several more suggestions
> below that use three different object creation APIs, including a 'new-and-
> constructor' version. Two of them also share the same sort of prototype-
> based object mechanism shared by your three samples.
>
>> These ways are more suited to cases where several
>> objects of the same kind are to be created, or where the code might be
>> reused in other projects. Several other ways exist but they are more
>> suited to the ad hoc creation of only one object.
>
> I believe all three techniques I suggest below are similarly suited to
> the creation of several objects of the same kind. The second one
> (`thing5`) is more memory-intensive, and probably is not suitable for
> hundreds of thousands or millions of instances of the type, but other
> than that, they have similar characteristics. `thing5` does offer the
> ability to have truly private implementation details, which many other
> techniques really don't.
I think it's unfortunate that ES2015 introduced a new incompatible `class`
syntax but missed the chance to address the very common need for private
data properties (or any declared properties, for that matter). Something
like that might be added in a future expansion of the standard, but
currently there are only stage 0 and stage 1 proposals. I found two relevant
ones; neither of them is included in the prospective feature set for ES2017:
https://github.com/jeffmo/es-class-fields-and-static-properties
https://github.com/wycats/javascript-private-state
There are workarounds: two well-established patterns (using naming
conventions and adding per-instance methods in the constructor) and two new
methods involving WeakMaps and Symbols. Maybe we should add those to the
list, as long as we're doing comparisons.
Things 4 and 5 in your examples support private data through the use of
per-instance methods, but (as you said) this comes at a price:
rss heapUsed heapTotal (ms)
Thing1 49.76 21.80 47.24 64
Thing2 49.76 21.80 47.24 65
Thing3 49.44 20.00 47.24 67
Thing4 170.85 142.51 166.32 990
thing5 181.99 160.44 178.13 425
Thing6 49.84 25.28 48.22 108
The numbers are memory usage in MB after creating 200k objects (using
Node.js 5.8.0 on my laptop; the usual caveats about platform-dependent
benchmarks and measurements apply).
> John Harris wrote:
>> Concerning way 1, 2, and 3
>> The three ways create objects that do much the same job. Which way is
>> best in general is a matter of personal preference. There is no reason
>> for loud and dogmatic assertions that one way is the Only True Way.
Well said.
- stefan
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-03 16:09 +0100
Re: Comparing some of the ways to create objects Scott Sauyet <scott@sauyet.com> - 2016-04-04 04:15 +0000
Re: Comparing some of the ways to create objects Stefan Weiss <krewecherl@gmail.com> - 2016-04-04 15:20 +0200
Re: Comparing some of the ways to create objects Scott Sauyet <scott@sauyet.com> - 2016-04-05 04:04 +0000
Re: Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-04 16:48 +0100
Re: Comparing some of the ways to create objects Scott Sauyet <scott@sauyet.com> - 2016-04-05 04:04 +0000
Re: Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-07 15:07 +0100
Re: Comparing some of the ways to create objects Scott Sauyet <scott@sauyet.com> - 2016-04-08 01:10 +0000
Re: Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-08 14:55 +0100
Re: Comparing some of the ways to create objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-04 22:12 +0200
Re: Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-05 09:57 +0100
Re: Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-06 16:41 +0100
Re: Comparing some of the ways to create objects Scott Sauyet <scott@sauyet.com> - 2016-04-06 23:42 +0000
Re: Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-07 16:09 +0100
Re: Comparing some of the ways to create objects Scott Sauyet <scott@sauyet.com> - 2016-04-08 02:05 +0000
Re: Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-08 15:43 +0100
Re: Comparing some of the ways to create objects Scott Sauyet <scott@sauyet.com> - 2016-04-09 00:01 +0000
Re: Comparing some of the ways to create objects Scott Sauyet <scott@sauyet.com> - 2016-04-09 16:46 +0000
Re: Comparing some of the ways to create objects Scott Sauyet <scott@sauyet.com> - 2016-04-10 16:51 +0000
Re: Comparing some of the ways to create objects Stanimir Stamenkov <s7an10@netscape.net> - 2016-05-08 17:27 +0300
Re: Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-10 11:31 +0100
Re: Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-10 11:52 +0100
csiph-web