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

determine object's pointer name

P: 8
Expand|Select|Wrap|Line Numbers
  1. mywidget = new Widget()
  2. alert(mywidget) // object Object
  3. alert(mywidget.toString()) // object Object
  4. alert(mywidget+"") // object Object
  5. alert(String(mywidget)) // object Object
  6.  
I have always wondered if there was a way to get the string "mywidget".

A potential use could avoid things like this:

Expand|Select|Wrap|Line Numbers
  1. Widget.widgetcount = 0
  2. function Widget() {
  3.    this.name = "mywidget"+(Widget.widgetcount++);
  4.    document.getElementsByTagName('body').item(0).appendChild(makeDiv(this.name+'Div'));
  5. }
  6. function WidgetTwo(name) {
  7.     document.getElementsByTagName('body').item(0).appendChild(makeDiv(name+'Div'));
  8. }
  9. function makeDiv(newID) {
  10.     var newDiv=document.createElement("DIV");
  11.     newDiv.id = newID;
  12.     return newDiv;
  13. }
  14. homer = new Widget()
  15. marge = new Widget()
  16. bart = new WidgetTwo("bart")
  17. lisa = new WidgetTwo("lisa")
  18.  
then the css would look like this:

Expand|Select|Wrap|Line Numbers
  1. #mywidget0Div {
  2. }
  3. #mywidget1Div {
  4. }
  5. //and
  6. #bartDiv {
  7. }
  8. #lisaDiv {
  9. }
  10.  
The first style forces the naming convention on you, the second style has redundancy. What I have always hoped for is:

Expand|Select|Wrap|Line Numbers
  1. function WidgetThree() {
  2.    this.name = magicalFunctionThatReturnsPointerNameAsString()
  3.    document.getElementsByTagName('body').item(0).appendChild(makeDiv(this.name+'Div'));
  4. }
  5. maggie = new WidgetThree()
  6.  
  7.  
  8.  
  9. #maggieDiv {
  10. }
  11.  
Is there any way to do this?
Sep 19 '06 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
I don't think so, though you could possibly use the window object and loop through its child objects. WidgetTwo seems like the best approach.
May 13 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.