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


Groups > comp.lang.javascript > #30230

Re: Comparing some of the ways to create objects

From John Harris <niam@jghnorth.org.uk.invalid>
Newsgroups comp.lang.javascript
Subject Re: Comparing some of the ways to create objects
Date 2016-04-06 16:41 +0100
Organization A noiseless patient Spider
Message-ID <0ebagb5j5s77hr64fhi3vlpr64asu3gous@4ax.com> (permalink)
References <hbc2gbpvk5nk24b0qupps6cjqn8fm3ar5e@4ax.com> <ndspod$lhh$1@dont-email.me>

Show all headers | View raw


On Mon, 4 Apr 2016 04:15:10 -0000 (UTC), Scott Sauyet
<scott@sauyet.com> wrote:

  <snip>
>My techniques follow.  I'm wondering if you see these ones as somehow 
>lesser techniques than the three you present.  And if so, why is that?

In general, any technique for the creation of one object can be
wrapped in a function and used in several places and reused in other
projects. The question is : does the function provided something extra
that constructors can't provide *and* is this needed here? If not then
an ordinary constructor is likely to be simpler and easier to read,
and sometimes faster.


>=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> 
>   function Thing4(phrase) {
>      var count = 0;
>      this.toString = function() {return phrase;}
>      this.incr = function() {count++;}
>      Object.defineProperties(this, {text: {
>        get: function() {return phrase;}
>      }, count: {
>        get: function() {return count;}
>      }})
>    } // Thing4
>
>    var a4 = new Thing4("Hello from Thing4 a4");
>    a4 + ': ' + a4.count; //=> "Hello from Thing4 a4: 0"
>    a4.incr();
>    a4 + ': ' + a4.count; //=> "Hello from Thing4 a4: 1"
>    var b4 = new Thing4("Hello from Thing4 b4");
>    b4 + ': ' + b4.count; //=> "Hello from Thing4 b4: 0"

This makes count and phrase private variables and defines getters for
them. I'm not a fan of private variables, getters, and setters unless
they are really really needed. If your programmers include someone who
can't stop corrupting objects while the program is running then get
rid of them. On the other hand, in a library if someone corrupts your
object then it's your fault : the customer is always right, i.e shouts
louder :-(


>    function thing5(phrase) {
>      var count = 0;
>      return {
>        toString: function() {return phrase;},
>        incr: function() {count++},
>        get count() {return count;}
>      };
>    } // thing5
>
>    var a5 = thing5("Hello from thing5 a5");
>    a5 + ': ' + a5.count; //=> "Hello from thing5 a5: 0"
>    a5.incr();
>    a5 + ': ' + a5.count; //=> "Hello from thing5 a5: 1"
>    var b5 = thing5("Hello from thing5 b5");
>    b5 + ': ' + b5.count; //=> "Hello from thing5 b5: 0"

ditto.


>    var Thing6 = {
>      init: function(phrase) {
>        this.text = phrase;
>        this.count = 0;
>      },
>      incr: function() {
>        this.count++;
>      },
>      toString: function() {
>        return this.text ;
>      }
>    }; // Thing6
>
>    var a6 = Object.create(Thing6);
>    a6.init("Hello from Thing6 a6");
>    a6 + ': ' + a6.count; //=> "Hello from Thing6 a6: 0"
>    a6.incr();
>    a6 + ': ' + a6.count; //=> "Hello from Thing6 a6: 1"
>    var b6 = Object.create(Thing6);
>    b6.init("Hello from Thing6 b6");
>    b6 + ': ' + b6.count; //=> "Hello from Thing6 b6: 0"

I C++ circles, i.e in comp.lang.c++.moderated, an initialise function
provokes cries of Eugh! It's annoying to have to write two things.
More important, it's too easy to forget the init or to miss it out
when copying and pasting elsewhere.

Really, all this example does is to implement a constructor function.
Why bother?

No doubt someone (not me) will complain that "Thing6" is not a
construction function so should not start with a capital letter. 

>=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

  John

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