473,800 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting var name in constructor

If I do something like this:

Cnst = function() {
// blah
}
Cnst.prototype. meth = function() {
// blah
}
var foo = new Cnst()

how can I get the name of the var (in this case 'foo'), or at least some
reference to it, when I'm within one of the constructor's prototypes?
Something like:

Cnst.prototype. meth = function() {
this.var = ???
}
Andrew Poulos

Jul 23 '05 #1
4 1455
Andrew Poulos wrote:
If I do something like this:

Cnst = function() {
// blah
}
Cnst.prototype. meth = function() {
// blah
}
var foo = new Cnst()

how can I get the name of the var (in this case 'foo'), or at least some
reference to it, when I'm within one of the constructor's prototypes?
Something like:

Cnst.prototype. meth = function() {
this.var = ???
}


Pass it to the constructor as a parameter and save it:

var foo = new Cnst('foo');

In general there may not be a variable at all, or there could be several,
so there isn't any mechanism for determining which variables refer to an
object short of testing the values of each of the possible variables.
Jul 23 '05 #2
Duncan Booth wrote:
Andrew Poulos wrote:

If I do something like this:

Cnst = function() {
// blah
}
Cnst.prototyp e.meth = function() {
// blah
}
var foo = new Cnst()

how can I get the name of the var (in this case 'foo'), or at least some
reference to it, when I'm within one of the constructor's prototypes?
Something like:

Cnst.prototyp e.meth = function() {
this.var = ???
}


Pass it to the constructor as a parameter and save it:

var foo = new Cnst('foo');

In general there may not be a variable at all, or there could be several,
so there isn't any mechanism for determining which variables refer to an
object short of testing the values of each of the possible variables.


I was kind of hoping that I didn't have to add a parameter for
something, I feel, the constructor/method would already know.

At any one time isn't there only one specific instance(?) that calls a
method?

Andrew Poulos
Jul 23 '05 #3
Andrew Poulos wrote:
Pass it to the constructor as a parameter and save it:

var foo = new Cnst('foo');

In general there may not be a variable at all, or there could be
several, so there isn't any mechanism for determining which variables
refer to an object short of testing the values of each of the
possible variables.


I was kind of hoping that I didn't have to add a parameter for
something, I feel, the constructor/method would already know.

At any one time isn't there only one specific instance(?) that calls a
method?


Yes, but an instance is an object, and a variable is simply something that
refers to an object. Think of a variable as a yellow sticky not with an
arrow coming out of it. Assigning a variable means you put the sticky note
so it points at the object, but there isn't anything on the object which
points back.

If you do this, then foo and bar are the same object, and methods just get
the object passed to them, not the variable:

var foo = new Cnst();
var bar = foo;
var baz = [foo, foo, foo];

bar.meth();
foo.meth();
baz[2].meth();

Once inside meth all of these calls are identical.

Also, how about:

alert('look no variables! '+ new Cnst().meth());
Jul 23 '05 #4
Andrew Poulos wrote:
how can I get the name of the var (in this case 'foo'), or at least
some reference to it, when I'm within one of the constructor's
prototypes?


The important question is - why would you want to?

Variable names are not important. They are lost when the code is compiled
anyway.

If you want a reference inside your object to point back to itself, keep an
array of those objects globally, and inside the constructor set a value to
point to which index the instance is.

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #5

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

Similar topics

3
59255
by: Pavils Jurjans | last post by:
Hallo, Is there some decent way how to get the object class name in a string format? Currently I use this: function getClassName(obj) { if (typeof obj != "object" || obj === null) return false; return /(\w+)\(/.exec(obj.constructor.toString());
6
3698
by: Robert | last post by:
Hello all... In my code below, the Notify Constructor and Destructor is getting called twice and it appears that a new Notify object is created on the 2nd call. The 2nd call is caused by this line below: pNotify = new EMAILnotify; that lives in the Notification Constructor. One theory is that the Notify base class is not completely constructed prior to using it in the Notification Constructor code: pNotify = new EMAILnotify;
11
3045
by: Andrew Thompson | last post by:
I have written a few scripts to parse the URL arguments and either list them or allow access to the value of any parameter by name. <http://www.physci.org/test/003url/index.html> <http://www.physci.org/test/003url/index.html?url=http://mybiz.com/&that=this&when=now#21> <http://www.physci.org/test/003url/index.html?url=http://mybiz.com/&when=now> Before I go offering it in public (and writing it into any number of the 'development kits'...
6
22539
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object instance (in this case 'myDog'). Of course it would be better if I could somehow know from within write() that the name of the object instance was 'myDog' without having to pass it as a parameter. //////////////////////////////// function...
3
1042
by: Tom Szabo | last post by:
Hi All, Just wondering if it is possible to get the name and class name of an object? oC = new control("name", "something"); .. oAnRef = oC;. ..
1
5072
by: Prasad Karunakaran | last post by:
I am using the C# DirectoryEntry class to retrieve the Properties of an user object in the Active Directory. I need to get the First Name and Last Name as properties. I know it is not supported with the ADSI NT Provider and only supported in the LDAP Provider. So given an UserId (UID) how can I read the First Name and Last Name using LDAP Provider. If anybody can help me with a C# sample code it would of great help. Thanks in advance.
2
1738
by: Jeff Van Epps | last post by:
We've been unable to get events working going from C# to VJ++. We think that the C# component is being exposed properly as a ConnectionPoint, and the Advise() from the VJ++ side seems to be working, because the delegate on the C# side has 1 item in its invocation list after the Advise(). However when we try to make the callback, we get: System.NullReferenceException: Object reference not set to an instance of an object at...
5
13212
by: Michael Howes | last post by:
I'm writing a utility to manage a machines *local* accounts in c# I am getting all the users in a specific Group just fine but when I want to get some of the information on each user from their Properties collection I can't get the properties on some users. For example, I get all the users that are part of my machines Administrators Group. I get get the properties of the built in local Administrator account and some local IT account,...
7
2689
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file name based on the name of the VB6 application. A second choice would be a file name based on the # COM interface assembly. I have tried calling Assembly.GetCallingAssembly() but this fails when I use the VB6 client. Is there a way to get this...
0
10504
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10274
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10251
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10033
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7576
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.