Reusing class name as variable name

  • Thread starter Klaus Johannes Rusch
  • Start date
K

Klaus Johannes Rusch

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

Martin Honnen

Klaus said:
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.
 
V

Vincent van Beveren

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

Klaus Johannes Rusch

Vincent said:
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.
 
M

Michael Winter

On Mon, 19 Apr 2004 16:11:49 +0200, Vincent van Beveren

[fixed top-post]
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
 
R

Richard Cornford

Klaus said:
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.
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:-
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.
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.
 
V

Vincent van Beveren

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

Michael Winter

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.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top