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


Groups > comp.lang.javascript > #20126

Re: No existing object

Message-ID <1758819.r8tGRauTii@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-06-03 15:04 +0200
Subject Re: No existing object
Newsgroups comp.lang.javascript
References <L4SdnQc8Q-MTWzbMnZ2dnUVZ_uWdnZ2d@westnet.com.au> <2326659.jpX2h7uNEo@PointedEars.de> <daae1899-e02d-4924-b6f8-1dffe640ac6c@z8g2000yqd.googlegroups.com>

Show all headers | View raw


Scott Sauyet wrote:

> Thomas 'PointedEars' Lahn wrote:
>> [... Code samples elided ...]

.o(…)
 
> This version of the code seems simpler to me:
> 
>     var Foo = (function() {
>       var counter = 0;
> 
>       var Foo = function(el) {
>         this.elem = ...
>         ++counter;
>       };
> 
>       Foo.prototype.destroy = function() {
>         --counter;
>         if (counter <= 0) {
>           /* all done */
>         }
>       };
> 
>       return Foo;
>     }());

I would not use the same unqualified “Foo” identifier in different contexts, 
but otherwise ACK.  More important than simplicity is that this way 
“counter” cannot be increased other than by calling the constructor.

JFTR, JSX's Function.prototype.extend() can also be used then:

  var Foo = (function() {
    var _counter = 0;

    var _Foo = function(el) {
      this.elem = ...
      ++_counter;
    };

    _Foo.extend(null, {
      destroy: function() {
        --_counter;
        if (_counter <= 0) {
          /* all done */
        }
      }
    });

    return _Foo;
  }());

or

  var Foo = (function() {
    var _counter = 0;

    var _Foo = (function(el) {
      this.elem = ...
      ++_counter;
    }).extend(null, {
      destroy: function() {
        --_counter;
        if (_counter <= 0) {
          /* all done */
        }
      }
    });

    return _Foo;
  }());

JSX's Function.prototype.extend() is useful if you want to define several 
methods on a prototype object, because you DRY.  It also provides special 
properties and methods on the constructors and prototype objects thus 
extended.  (The underscores for private symbols are optional, but I consider 
that good style.)

-- 
PointedEars

Twitter: @PointedEars2
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


Thread

No existing object Andrew Poulos <ap_prog@hotmail.com> - 2013-06-03 08:26 +1000
  Re: No existing object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-03 01:24 +0200
    Re: No existing object Scott Sauyet <scott.sauyet@gmail.com> - 2013-06-03 05:45 -0700
      Re: No existing object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-03 15:04 +0200
        Re: No existing object Scott Sauyet <scott.sauyet@gmail.com> - 2013-06-03 10:04 -0700
  Re: No existing object John Harris <niam@jghnorth.org.uk.invalid> - 2013-06-03 14:30 +0100
    Re: No existing object Andrew Poulos <ap_prog@hotmail.com> - 2013-06-04 08:05 +1000
      Re: No existing object John Harris <niam@jghnorth.org.uk.invalid> - 2013-06-04 10:57 +0100

csiph-web