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


Groups > comp.lang.javascript > #20118 > unrolled thread

No existing object

Started byAndrew Poulos <ap_prog@hotmail.com>
First post2013-06-03 08:26 +1000
Last post2013-06-04 10:57 +0100
Articles 8 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

#20118 — No existing object

FromAndrew Poulos <ap_prog@hotmail.com>
Date2013-06-03 08:26 +1000
SubjectNo existing object
Message-ID<L4SdnQc8Q-MTWzbMnZ2dnUVZ_uWdnZ2d@westnet.com.au>
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? Should I just put a counter into the constructor and then 
decrement it when reset is called?

Andrew Poulos

[toc] | [next] | [standalone]


#20121

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-06-03 01:24 +0200
Message-ID<2326659.jpX2h7uNEo@PointedEars.de>
In reply to#20118
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.

[toc] | [prev] | [next] | [standalone]


#20123

FromScott Sauyet <scott.sauyet@gmail.com>
Date2013-06-03 05:45 -0700
Message-ID<daae1899-e02d-4924-b6f8-1dffe640ac6c@z8g2000yqd.googlegroups.com>
In reply to#20121
Thomas 'PointedEars' Lahn wrote:
> 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.

ACK

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

That is definitely the name more in keeping with other OO languages.

This technique is possible, but is never pleasant.  Use it if you
absolutely have to know whether you have any such objects still
around, but I've generally found that when I thought I needed such a
technique, some simpler scheme eventually availed.


> [... Code samples elided ...]

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;
    }());

  -- Scott

[toc] | [prev] | [next] | [standalone]


#20126

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-06-03 15:04 +0200
Message-ID<1758819.r8tGRauTii@PointedEars.de>
In reply to#20123
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.

[toc] | [prev] | [next] | [standalone]


#20128

FromScott Sauyet <scott.sauyet@gmail.com>
Date2013-06-03 10:04 -0700
Message-ID<71161e09-2681-4666-91b9-0eaef935add1@w8g2000yqf.googlegroups.com>
In reply to#20126
Thomas 'PointedEars' Lahn wrote:
> Scott Sauyet wrote:
>> Thomas 'PointedEars' Lahn wrote:
>>> [... Code samples elided ...]
>
> .o(…)

I'm afraid I don't recognize that notation...

>> This version of the code seems simpler to me:
>>
>>     var Foo = (function() {
>>       // ...
>>       var Foo = function(el) { /*... */};
>>       // ...
>>       return Foo;
>>     }());
>
> I would not use the same unqualified “Foo” identifier in different contexts,

I have never come to a strong conclusion on this.  Sometimes the reuse
of the identifier for the object to which it will end up being
assigned seems not only elegant but as clear as can be.  Other times
I'm afraid I might simply be confusing someone.  Do you have a strong
argument to make on the subject?



> but otherwise ACK.  More important than simplicity is that this way
> “counter” cannot be increased other than by calling the constructor.

Of course.


> 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;
>   }());

A URL for JSX's `Function.prototype.extend` would be welcome.


>   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;
>   }());

Is there any reason that you, as the author of JSX, would suggest to
prefer one of these two styles over the other?



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

What sorts of properties and methods?  Magic makes me nervous!  :-)


>            (The underscores for private symbols are optional, but I consider
> that good style.)

I've never embraced that convention, but it's certainly not uncommon.
Do you use other common prefixes as well to distinguish other types of
symbols?

  -- Scott

[toc] | [prev] | [next] | [standalone]


#20127

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2013-06-03 14:30 +0100
Message-ID<5i6pq81comf9sn0vdmr6ih2u5a9n3f1tls@4ax.com>
In reply to#20118
On Mon, 03 Jun 2013 08:26:20 +1000, Andrew Poulos
<ap_prog@hotmail.com> wrote:


  <snip>
>How can I know if there are no objects constructed from Foo still 
>existing? Should I just put a counter into the constructor and then 
>decrement it when reset is called?

Obvious question : why would you want to do that ?

The problem is that you would be making foo's code, and the foo
objects, unusable elsewhere in the program. So much for OO making code
more reusable.

To put it another way : are you so sure that you will never, ever,
want two separate collections of foo objects ? 

Would you ever want to seek and destroy all the Date objects in a
program ?

  John

[toc] | [prev] | [next] | [standalone]


#20132

FromAndrew Poulos <ap_prog@hotmail.com>
Date2013-06-04 08:05 +1000
Message-ID<UrSdnblGccSjjjDMnZ2dnUVZ_vednZ2d@westnet.com.au>
In reply to#20127
On 3/06/2013 11:30 PM, John Harris wrote:
> On Mon, 03 Jun 2013 08:26:20 +1000, Andrew Poulos
> <ap_prog@hotmail.com> wrote:
>
>
>    <snip>
>> How can I know if there are no objects constructed from Foo still
>> existing? Should I just put a counter into the constructor and then
>> decrement it when reset is called?
>
> Obvious question : why would you want to do that ?
>
> The problem is that you would be making foo's code, and the foo
> objects, unusable elsewhere in the program. So much for OO making code
> more reusable.
>
> To put it another way : are you so sure that you will never, ever,
> want two separate collections of foo objects ?
>
> Would you ever want to seek and destroy all the Date objects in a
> program ?

I'm unsure what you're saying as I didn't say I wanted to seek and 
destroy objects. I was asking if its possible to know if all objects had 
been destroyed.

Andrew Poulos

[toc] | [prev] | [next] | [standalone]


#20140

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2013-06-04 10:57 +0100
Message-ID<c6erq8p7qvk7b0j6tholihe5ohd52u0eqc@4ax.com>
In reply to#20132
On Tue, 04 Jun 2013 08:05:43 +1000, Andrew Poulos
<ap_prog@hotmail.com> wrote:

>On 3/06/2013 11:30 PM, John Harris wrote:
>> On Mon, 03 Jun 2013 08:26:20 +1000, Andrew Poulos
>> <ap_prog@hotmail.com> wrote:
>>
>>
>>    <snip>
>>> How can I know if there are no objects constructed from Foo still
>>> existing? Should I just put a counter into the constructor and then
>>> decrement it when reset is called?
>>
>> Obvious question : why would you want to do that ?
>>
>> The problem is that you would be making foo's code, and the foo
>> objects, unusable elsewhere in the program. So much for OO making code
>> more reusable.
>>
>> To put it another way : are you so sure that you will never, ever,
>> want two separate collections of foo objects ?
>>
>> Would you ever want to seek and destroy all the Date objects in a
>> program ?
>
>I'm unsure what you're saying as I didn't say I wanted to seek and 
>destroy objects. I was asking if its possible to know if all objects had 
>been destroyed.

It's the "all" in "all objects" that I'm questioning. You might want
to know if all the foo objects in your part of the program have been
dealt with, but what about foo objects in someone else's part of the
program. How would you avoid counting their objects ?

It seems to me that you're building a collection with some management
actions. Why not implement it as an object holding a collection of foo
objects and some suitable methods. Then you can have more than one
collection without them interfering with each other. Now you can
contemplate putting the code into a library for future use.

  John

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web