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());