HighDots Forums  

Reusing class name as variable name

Javascript JavaScript language (comp.lang.javascript)


Discuss Reusing class name as variable name in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Klaus Johannes Rusch
 
Posts: n/a

Default Reusing class name as variable name - 04-19-2004 , 07:14 AM







Is the following code valid and supported by current implementations?

function somename() {
this.show = function () { document.write("somename called") }
}

var somename = new somename();
somename.show()

Note that the class name "somename" is reused for the variable name.

--
Klaus Johannes Rusch
KlausRusch (AT) atmedia (DOT) net
http://www.atmedia.net/KlausRusch/

Reply With Quote
  #2  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Reusing class name as variable name - 04-19-2004 , 09:11 AM








Klaus Johannes Rusch wrote:

Quote:
Is the following code valid and supported by current implementations?

function somename() {
this.show = function () { document.write("somename called") }
}

var somename = new somename();
somename.show()

Note that the class name "somename" is reused for the variable name.
You can do the above but after the statement
var somename = new somename();
has been executed there no longer is a global function of the name
'somename' thus any further
new somename()
will fail with an error.

--

Martin Honnen
http://JavaScript.FAQTs.com/



Reply With Quote
  #3  
Old   
Vincent van Beveren
 
Posts: n/a

Default Re: Reusing class name as variable name - 04-19-2004 , 09:11 AM



No, its invalid, because JavaScript does not have classes, only objects.
A 'function myclass()' would create a function (which is an object)
myclass capable of building and returning objects.

You can see this by attempting to do the following after your code.

someother = new somename();
someother.show();

would result in an error, because it would attempt to build an object
from something that is now another object.




Klaus Johannes Rusch wrote:

Quote:
Is the following code valid and supported by current implementations?

function somename() {
this.show = function () { document.write("somename called") }
}

var somename = new somename();
somename.show()

Note that the class name "somename" is reused for the variable name.



Reply With Quote
  #4  
Old   
Klaus Johannes Rusch
 
Posts: n/a

Default Re: Reusing class name as variable name - 04-19-2004 , 10:12 AM



Vincent van Beveren wrote:

Quote:
No, its invalid, because JavaScript does not have classes, only objects.
A 'function myclass()' would create a function (which is an object)
myclass capable of building and returning objects.

You can see this by attempting to do the following after your code.

someother = new somename();
someother.show();

would result in an error, because it would attempt to build an object
from something that is now another object.
Surely this would fail, the question is if reusing the name of the
function that allows creating an object of that type (which is what I
meant by "class" ;-)) when assigning this to a variable once and only once.

I cannot think of a reason why this should not work but would like to
solicit opinions before actually using this.

--
Klaus Johannes Rusch
KlausRusch (AT) atmedia (DOT) net
http://www.atmedia.net/KlausRusch/


Reply With Quote
  #5  
Old   
Michael Winter
 
Posts: n/a

Default Re: Reusing class name as variable name - 04-19-2004 , 10:26 AM



On Mon, 19 Apr 2004 16:11:49 +0200, Vincent van Beveren
<vincent (AT) provident (DOT) remove.this.nl> wrote:

[fixed top-post]

Quote:
Klaus Johannes Rusch wrote:

Is the following code valid and supported by current implementations?

No, its invalid, [...]
"Invalid" is too strong, and incorrect. Unwise might be better, but it
depends on what the OP is trying to accomplish.

To the OP: If you are trying to create an object via an anonymous function
(that is, make the constructor unavailable), you might want to try:

var somename = new ( function() {
/* constructor code */

this.show = function {
/* your code */
};
});

/* ... */

somename.show();

Alternatively,

var somename = ( function() {
function myObject() {
/* constructor code */
}
myObject.show = function() {
/* your code */
};
return myObject;
})();

or, in a slight variation of the above:

var somename = ( function() {
return({
show:function() {
/* your code */
}
});
})();

Mike

--
Michael Winter
M.Winter (AT) blueyonder (DOT) co.invalid (replace ".invalid" with ".uk" to reply)


Reply With Quote
  #6  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Reusing class name as variable name - 04-19-2004 , 11:12 AM



Klaus Johannes Rusch wrote:
Quote:
Is the following code valid and supported by current implementations?
Yes, but the chances that you actually want to do this are close to
zero.

The execution of global code commences with "variable instantiation"
using the global object as the Activation/Variable object in the global
execution context.

First function declarations are evaluated and named properties created
on the Variable object with names corresponding to the function names
used and then references to the function objects created using the
function definitions are assigned to those named properties.

Quote:
function somename() {
this.show = function () { document.write("somename called") }
}
So at this stage the global object has a property named "somename" that
refers to a function object.

The next stage in variable instantiation is to create named properties
of the Variable object for each declared (with the - var - keyword)
local variable (local variables in global code become global variables).
However, if the Variable object already has a named property
corresponding with the identifier used in a variable declaration no new
property is created and its original value is unaltered. So - var
somename - does not result in the creation of a new property of the
Variable object, and the existing "somename" property of that object
continues to refer to the declared function.

That is all of the variable instantiation required by this code so
execution of the global code will follow.

The first statment:-

Quote:
var somename = new somename();
- resolves the right had side first, the identifier - somename - is
subject to resolutin against the scope chain, which includes the
Variable object for the global execution context so - somename - is
resolved as a property of that object, the property referring to the
function object created with the function declaration.

The - new - operator calls that funciton as a constructor and a
reference to an object is returned. So the right hand side of the
assignment expression evaluates as a reference to an object.

Next the assignment is performed. To know where to assign the value the
left hand side the identifier - somename - is scope chain resolved,
again as a reference to a named property of the Variable object. And the
reference to an object that was the evaluated result of the right hand
side expression is assigned to that named property of the Variable
object. Replacing the reference to the function object that had
previously been the value of that property.

As a result any further attempts to use the identifier - somename - in a
function or constructor context will result in an error as it now refers
to an object.

Quote:
somename.show()

Note that the class name "somename" is reused for the variable name.
But only one named property of the Variable object for the global
execution context is created, and that property can only hold one value
at a time.

Richard.




Reply With Quote
  #7  
Old   
Vincent van Beveren
 
Posts: n/a

Default Re: Reusing class name as variable name - 04-20-2004 , 09:43 AM



Well, maybe not invalid, but I can't really think of any circumstances
this would really be useful. It won't make things more clear, thats for
sure. The only reason might be obfuscation and making things
unreachable.


Reply With Quote
  #8  
Old   
Michael Winter
 
Posts: n/a

Default Re: Reusing class name as variable name - 04-20-2004 , 11:44 AM



On Tue, 20 Apr 2004 16:43:40 +0200, Vincent van Beveren
<vincent (AT) provident (DOT) remove.this.nl> wrote:

Quote:
Well, maybe not invalid, but I can't really think of any circumstances
this would really be useful. It won't make things more clear, thats for
sure. The only reason might be obfuscation and making things
unreachable.
The OP e-mailed me and said the goal was to make the constructor
unavailable once the object had been initialised. A one-off constructor.
He decided to use one of the patterns I had shown in my post.

Mike


Please make sure you quote the relevant part of the post you're responding
to. Others might not know to whom you were responding. If it was me,
either your newsreader's broken, or you didn't actually reply to my post.

--
Michael Winter
M.Winter (AT) blueyonder (DOT) co.invalid (replace ".invalid" with ".uk" to reply)


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.