By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,625 Members | 1,720 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,625 IT Pros & Developers. It's quick & easy.

Getting var name in constructor

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
Duncan Booth wrote:
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.


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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.