Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #30236

Re: Comparing some of the ways to create objects

From Scott Sauyet <scott@sauyet.com>
Newsgroups comp.lang.javascript
Subject Re: Comparing some of the ways to create objects
Date 2016-04-08 02:05 +0000
Organization A noiseless patient Spider
Message-ID <ne73l3$2i4$2@dont-email.me> (permalink)
References <hbc2gbpvk5nk24b0qupps6cjqn8fm3ar5e@4ax.com> <ndspod$lhh$1@dont-email.me> <0ebagb5j5s77hr64fhi3vlpr64asu3gous@4ax.com> <ne46s8$8td$1@dont-email.me> <7vtcgb5hio1hbbtg7cg0ca1dmjkel0hv6b@4ax.com>

Show all headers | View raw


John Harris wrote:
> Scott Sauyet wrote:

>> That's what I was trying to get at.  Do you think that there is
>> something special about your original list, or was it simply the first
>> techniques that came to mind?
> 
> They are special in the sense that they are simple and straightforward.
> Other ways are more complicated or suffer from code bloat. If the
> software project is done by a few trustworthy people and the code is not
> going to be sold to others, then simplicity and lack of bloat win the
> day.

I would argue that in fact, a variant of my #5 is substantially simpler:

    function thing8(phrase) {
      return {
        count: 0,
        toString: function() {return phrase;},
        incr: function() {this.count++},
      };
    } // thing8

There are certainly legitimate reasons not to like this.  (See Stefan 
Weiss' response for some of them.)  But if simplicity is you chief 
concern, then this one beats them all.


> Unfortunately, private encapsulation is not an ECMAScript feature (yet).

But it is.  I offered two ways to achieve it.  Stefan also mentioned 
techniques involving Symbols and WeakMaps.  Again, my main point is that, 
if you're going to do OOP, there are many different techniques to achieve 
it.


>> By the way, this does not define getter-functions.
> 
> As usual, there's more than one way to do getting and setting. There's
> nothing wrong with calling them getters, especially when including ES3
> environments.

Just so long as no one confused the syntax with something like Java's 
JavaBeans structure...




> [ ... ]
> 
>> Still, my big question is whether you find these techniques somehow
>> inferior to your original list.
> 
> They are inferior when they are not needed.
> They are essential when they are needed.

Your Honor, permission to treat this man as a hostile witness?  :-)

To me, your list didn't offer ways to solve some of the most essential 
problems in OOP; instead it gave three barely-distinguishable versions of 
the same basic idea and seemed to claim that they covered everything 
important in OOP.


>> Do you believe your list captures the essence of the set of reasonable
>> object creation techniques in Javascript?
> 
> It captures the essence of the different ways, without complicating the
> picture. E.g The per-object data is shown in one list, not scattered
> around arguments and local variables.

Version #8 above does that more succinctly.  Should that be the only one 
we promote?


>>  [1]: <https://davidwalsh.name/javascript-objects-deconstruction>
>   <snip>
> 
> His preferred construction is your Thing6 example. It uses two
> statements to create a new object. He forgets that if you are going to
> make the same kind of object in several places then you would
> encapsulate the code in a makeThing6 function.

I seriously doubt he would object to a function which encapsulated this 
behavior.  His point was to demonstrate the difference in what's 
generated between several different Object-creation schemes.  He was not 
promoting a final API for OOP design.

In any case, I would personally choose something like #5 / #8 for most of 
my work.  I find this clearest and most expressive.  And I suppose that's 
why I challenge the notion that #1 - #3 are paticularly relevent.

  -- Scott

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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