Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #20121
| Message-ID | <2326659.jpX2h7uNEo@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2013-06-03 01:24 +0200 |
| Subject | Re: No existing object |
| Newsgroups | comp.lang.javascript |
| References | <L4SdnQc8Q-MTWzbMnZ2dnUVZ_uWdnZ2d@westnet.com.au> |
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
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