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


Groups > comp.lang.javascript > #20121

Re: No existing object

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!news.teledata-fn.de!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <2326659.jpX2h7uNEo@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <usenet@PointedEars.de>
Organization PointedEars Software (PES)
Date Mon, 03 Jun 2013 01:24:16 +0200
User-Agent KNode/4.10.2
Content-Transfer-Encoding 8Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Subject Re: No existing object
Newsgroups comp.lang.javascript
References <L4SdnQc8Q-MTWzbMnZ2dnUVZ_uWdnZ2d@westnet.com.au>
MIME-Version 1.0
Lines 106
NNTP-Posting-Date 03 Jun 2013 01:24:20 CEST
NNTP-Posting-Host 98dea18d.newsspool4.arcor-online.net
X-Trace DXC=17]`WVNogV3lIh70@<QIE74IUK<Cl32<14Fo<]lROoR18kF<OcfhCO;Qce:MKiA0>5DZm8W4\YJN<b@mF9jNikd57a5\<`>]gf>V8Y^iHGBo@>
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:20121

Show key headers only | View raw


Andrew Poulos wrote:

> If I have a constructor:
> 
> var Foo = function(el) {
>    this.elem =...
> };
> Foo.prototype.reset = function() {
>     ...
> };
> 
> and then its used like this
> 
> var a = new Foo("elem1");
> var b = new Foo("elem2");
> var c = new Foo("elem3");
> 
> ...
> a.reset();
> a = null;
> 
> How can I know if there are no objects constructed from Foo still
> existing?

You cannot.

> Should I just put a counter into the constructor and then decrement it
> when reset is called?

Depending on what this is for, putting a prototype-local counter (to 
restrict access), increasing it with each constructor call and decreasing it 
with each call of reset() might be a good idea.  I suggest naming “reset” 
“destroy” then, instead.

  function Foo ()
  {
    this.inc();
  }

  Foo.prototype = (function () {
    var counter = 0;
    
    var _Foo_prototype = {
      constructor: Foo,

      inc: function () {
        ++counter;
      },

      destroy: function () {
        --counter;

        if (counter <= 0)
        {
          /* all done */
        }
      }
    };

    if (Object.defineProperty == "function")
    {
      try
      {
        Object.defineProperty(_Foo_prototype, "constructor", {
          value: Foo
        });
      }
      catch (e)
      {
        /* borken implementation, like JScript 9.0 */
      }
    }

    return _Foo_prototype;
  }());

or with JSX:

  var Foo = (function () {
    this.inc();
  }).extend(null, (function () {
    var counter = 0;

    return {
      inc: function () {
        ++counter;
      },

      destroy: function () {
        --counter;

        if (counter <= 0)
        {
          /* all done */
        }
      }
    };
  }()));

Untested.

-- 
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