Uses for "self-instantiating classes"?

D

David Karr

I noticed a new JavaScript practice today that seems curious, and I'm
trying to imagine when you might use this. The book I saw this in
refers to it as "self-instantiating classes". I understand basically
how it works, but I've never seen this done before in this way, so I'm
wondering what use it might be.

The following is an excerpt that illustrates the practice (the key is
the "new" on the function):
------------------------
// Example of a self-instantiating class
var Inbox = new function() {
this.messageCount = 0;
this.refresh = function() {
return true;
}
}();
// The new keyword and braces force the function to immediately
execute,
// meaning the Inbox variable now contains the single object instance,
// not the class

// Example method call on the single instance of Inbox
Inbox.refresh();
----------------------

What happens here is that executing "Inbox.refresh()" first executes
the function that you might call "Inbox", apparently creating a new
instance of the function, then calls the "refresh()" function on that
object. I see what it does, it just seems unusual.
 
P

Peter Michaux

I noticed a new JavaScript practice today that seems curious, and I'm
trying to imagine when you might use this. The book I saw this in
refers to it as "self-instantiating classes". I understand basically
how it works, but I've never seen this done before in this way, so I'm
wondering what use it might be.

The following is an excerpt that illustrates the practice (the key is
the "new" on the function):
------------------------
// Example of a self-instantiating class
var Inbox = new function() {
this.messageCount = 0;
this.refresh = function() {
return true;
}}();

It seems like the author may program a lot with Java.

Isn't that code the same as

var Inbox = {
messageCount: 0,
refresh: function() {return true;}
};

(I probably wouldn't use a captital 'I' for the variable name as that
is used as a JavaScript convention for a constructor function. Inbox
does not reference a constructor function.)
What happens here is that executing "Inbox.refresh()" first executes
the function that you might call "Inbox", apparently creating a new
instance of the function, then calls the "refresh()" function on that
object.  I see what it does, it just seems unusual.

This interpretation seems off at least in some ways.

Peter
 
T

Thomas 'PointedEars' Lahn

David said:
I noticed a new JavaScript practice today that seems curious, and I'm
trying to imagine when you might use this. The book I saw this in
refers to it as "self-instantiating classes".

There are no classes, JavaScript (and other ECMAScript implementations used
client-side) so far uses prototype-based inheritance. Dump the book.
I understand basically how it works, but I've never seen this done
before in this way, so I'm wondering what use it might be.

The following is an excerpt that illustrates the practice (the key is
the "new" on the function):

It is an example of instantiation using an anonymous constructor instead.
var Inbox = new function() {
this.messageCount = 0;
this.refresh = function() {
return true;
}
}();
// The new keyword and braces force the function to immediately
execute,
True.

// meaning the Inbox variable now contains the single object instance,
// not the class
Wrong.

// Example method call on the single instance of Inbox
Inbox.refresh();
----------------------

What happens here is that executing "Inbox.refresh()" first executes
the function that you might call "Inbox", apparently creating a new
instance of the function, then calls the "refresh()" function on that
object. I see what it does, it just seems unusual.

You are mistaken. The `new function() { ... }()' creates a new object using
the constructor created by the anonymous function expression `function() {
.... }', whereas `()' is the constructor call's (empty) argument list, and
assigns a reference to it to `Inbox'. Afterwards, `Inbox.refresh()' calls
the method that was added to the object in its constructor.

Given that nothing else is done here, using the approach is a waste of
resources. For it can be rewritten as

var inbox = {
messageCount = 0;
refresh: function() {
return true;
}
};

However, if "private" properties were to be constructed, that approach would
be useful:

var inbox = new function() {
var _messageCount = 0;

this.getMessageCount = function() {
return _messageCount;
};

this.refresh = function() {
/* update _messageCount here */

return true;
};
}();

It is generally a Bad Idea and a sign of inexperience to let
non-constructable references start with a capital letter,
(pseudo-)constants and JScript .NET aside. Dump the book.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Given that nothing else is done here, using the approach is a waste of
resources. For it can be rewritten as

var inbox = {
messageCount = 0;
refresh: function() {
return true;
}
};

Correction:

var inbox = {
messageCount: 0,

refresh: function() {
return true;
}
};


PointedEars
 
T

Thomas 'PointedEars' Lahn

David said:
I noticed a new JavaScript practice today that seems curious, and I'm
trying to imagine when you might use this. The book I saw this in
refers to it as "self-instantiating classes". I understand basically
how it works, but I've never seen this done before in this way, so I'm
wondering what use it might be.
The following is an excerpt that illustrates the practice (the key is
the "new" on the function):
------------------------
// Example of a self-instantiating class
var Inbox = new function() {
this.messageCount = 0;
this.refresh = function() {
return true;
}}();
It seems like the author may program a lot with Java.

Isn't that code the same as

var Inbox = {
messageCount: 0,
refresh: function() {return true;}

};

[...]
I missed the fact originally that this is a "self-executing function",
indicated by the "()" at the end of the definition (it's easy to miss
those).

No, it isn't.
It's useful to duplicate the "Inbox.refresh()" line a couple of times
and watch Firebug step through the "Inbox" function each time "Inbox"
is referenced.

Utter nonsense.

Please trim your quotes. <http://jibbering.com/faq/#posting>


PointedEars
 
D

David Karr

It seems like the author may program a lot with Java.

Isn't that code the same as

var Inbox = {
  messageCount: 0,
  refresh: function() {return true;}

};

(I probably wouldn't use a captital 'I' for the variable name as that
is used as a JavaScript convention for a constructor function. Inbox
does not reference a constructor function.)


This interpretation seems off at least in some ways.

Peter

I missed the fact originally that this is a "self-executing function",
indicated by the "()" at the end of the definition (it's easy to miss
those).

It's useful to duplicate the "Inbox.refresh()" line a couple of times
and watch Firebug step through the "Inbox" function each time "Inbox"
is referenced.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top