Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the current object instance name from within itself?

Status
Not open for further replies.

Kindon

Programmer
Apr 19, 2001
54
0
0
US
I want to get the current instance name from within that same instansiated function.

function MyFunction() {
this.prop1 = 1;
this.prop2 = 2;

alert("My instance name: " + MyFunction.caller + "?");
}

var newFunc1 = new MyFunction();
var newFunc2 = new MyFunction();

===================================

The result should be:

"My instance name: newFunc1?"
"My instance name: newFunc2?"

===================================

The result is:

"My instance name: null?"
"My instance name: null?"

===================================


Can this be done somehow. Caller is not working as I expected. Is there some other way?

 
From the help:
[tt]
The caller property is only defined for a function while that function is executing. If the function is called from the top level of a JScript program, caller contains null.
[/tt]

That said... newFunc1 and newFunc2 are not even functions - they're variables.

Unfortunately, I'm not sure there is a way to get the name of the variable in this manner.

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
There's nothing stopping you from adding a Name property to your function.

function MyFunction(sName) {
this.prop1 = 1;
this.prop2 = 2;
this.caller = sName;

alert("My instance name: " + MyFunction.caller + "?");
}

var newFunc1 = new MyFunction("newFunc1");
var newFunc2 = new MyFunction("newFunc2");


 
Yeah. I have done that....

I was looking for a way to do it internally. Thanks anyway though....

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top