determine object's pointer name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abducted
    New Member
    • Sep 2006
    • 8

    determine object's pointer name

    Code:
    mywidget = new Widget()
    alert(mywidget) // object Object
    alert(mywidget.toString()) // object Object
    alert(mywidget+"") // object Object
    alert(String(mywidget)) // object Object
    I have always wondered if there was a way to get the string "mywidget".

    A potential use could avoid things like this:

    Code:
    Widget.widgetcount = 0
    function Widget() {
       this.name = "mywidget"+(Widget.widgetcount++);
       document.getElementsByTagName('body').item(0).appendChild(makeDiv(this.name+'Div'));
    }
    function WidgetTwo(name) {
        document.getElementsByTagName('body').item(0).appendChild(makeDiv(name+'Div'));
    }
    function makeDiv(newID) {
        var newDiv=document.createElement("DIV");
        newDiv.id = newID;
        return newDiv;
    }
    homer = new Widget()
    marge = new Widget()
    bart = new WidgetTwo("bart")
    lisa = new WidgetTwo("lisa")
    then the css would look like this:

    Code:
    #mywidget0Div {
    }
    #mywidget1Div {
    }
    //and
    #bartDiv {
    }
    #lisaDiv {
    }
    The first style forces the naming convention on you, the second style has redundancy. What I have always hoped for is:

    Code:
    function WidgetThree() {
       this.name = magicalFunctionThatReturnsPointerNameAsString()
       document.getElementsByTagName('body').item(0).appendChild(makeDiv(this.name+'Div'));
    }
    maggie = new WidgetThree()
    
    
    
    #maggieDiv {
    }
    Is there any way to do this?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    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.

    Comment

    Working...