473,577 Members | 3,319 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reusing class name as variable name


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
Kl********@atme dia.net
http://www.atmedia.net/KlausRusch/
Jul 23 '05 #1
7 1973
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:

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.


Jul 23 '05 #2


Klaus Johannes Rusch wrote:
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/

Jul 23 '05 #3
Vincent van Beveren wrote:
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
Kl********@atme dia.net
http://www.atmedia.net/KlausRusch/
Jul 23 '05 #4
On Mon, 19 Apr 2004 16:11:49 +0200, Vincent van Beveren
<vi*****@provid ent.remove.this .nl> wrote:

[fixed top-post]
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.******@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #5
Klaus Johannes Rusch wrote:
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.
Jul 23 '05 #6
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.

Jul 23 '05 #7
On Tue, 20 Apr 2004 16:43:40 +0200, Vincent van Beveren
<vi*****@provid ent.remove.this .nl> wrote:
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.******@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1683
by: Ivan Voras | last post by:
Can this be done: (this example doesn't work) ---- class A: def a_lengthy_method(self, params): # do some work depending only on data in self and params class B:
7
13203
by: Robin Forster | last post by:
I have two classes: aule_gl_window (parent class) and aule_button (sub class) I want to call the super class (parent) constructor code from the sub class constructor.
3
3164
by: Alan Krueger | last post by:
Greetings, I've been able to cache Transformer objects in a Tomcat-based servlet application to avoid unnecessary Transformer rebuilding, except for certain ones on certain machines. I'm running Tomcat 4.1.27 under Eclipse 2.1.0 using the Sysdeo Tomcat plugin using j2re1.4.1_02 under Windows 2000 SP4. I've digested this down to a small...
4
4225
by: Jon | last post by:
I have links that open https secondary windows using: window.open(url, windowName); The windowName is always the same but I keep getting new windows. Is it possible for each of the links to open in the same window? Thanks, Jon
9
2345
by: Alan | last post by:
Using VC++ (1998) compiler with PFE32 editor in Win2K Pro SP4. (DigitalMars CD on order. ) The program (below) instantiates a class and then deletes it. I would have thought that reusing the deleted pointer would have caused an exception, but it does not - or my coding or thinking is incorrect? #include <iostream> using namespace std;
2
1362
by: Luis | last post by:
I have a function sililar to the one below on one of my pages. It puts the value of a field on a form into a variable called FieldName: function checkSomething() { var FieldName = document.frmFormName.FieldName.value } I'd like to reuse the function on a different page (instead of writing another almost identical script on that page),...
4
1256
by: Rob Meade | last post by:
Hi all, I'm in the middle of finishing a page and I've noticed that I have a chunk of code that is used in two places - its exactly the same - I guess I can rip this out - save it seperately and then call it into both areas when needed - is this a 'class' - I'm not fully up with all the terminology... So what should I do - within the same...
1
1320
by: karye2004 | last post by:
Hi! I'm trying to access python objects from c++. It works once. Second time it hangs. Does someone have any clue or some example code or pointer? Thanks! /Karim Here are some python classes:
0
1005
by: rlwebbnafex | last post by:
Hello, I have my inline class definition below. I have my member template constructors which initialize my "data" object. The problem is the CheckOne() function, which uses the T1 template argument. I want it to iterate over "data" with the same T1 datatype that was used to construct the data object in the IndividualRecord constructor. That...
0
7846
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7777
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8289
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8145
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6519
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5341
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1388
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.