object's name (anti-eval)

Discussion in 'Javascript' started by Oleg Alistratov, Nov 5, 2004.

  1. Is in javascript a method to get object's identifier as string?
    E.g., may I write function GetName thus, that

    var v;
    var s = GetName(v);

    has led to

    s == "v"

    Please, help.
    Oleg Alistratov, Nov 5, 2004
    #1
    1. Advertising

  2. Oleg Alistratov

    Lee Guest

    Oleg Alistratov said:
    >
    >Is in javascript a method to get object's identifier as string?
    >E.g., may I write function GetName thus, that
    >
    > var v;
    > var s = GetName(v);
    >
    >has led to
    >
    > s == "v"


    No. It would make no sense, since the same object can have many
    different names. Also, if you think you need an object's name,
    you are probably making a design mistake of some sort.
    Lee, Nov 5, 2004
    #2
    1. Advertising

  3. > Is in javascript a method to get object's identifier as string?
    > E.g., may I write function GetName thus, that
    >
    > var v;
    > var s = GetName(v);
    >
    > has led to
    >
    > s == "v"


    You are correct in wanting to avoid eval. Generally, an object's name is
    irrelevant because you are working with object references, not object
    names. If you need object names in order to avoid learning to use object
    references, then you should obtain a better book. See
    http://www.crockford.com/javascript/javascript.html

    Names can be useful in some applications. In those cases, keep the
    objects in a little database.

    var database = {};
    function MyObject(name) {
    this.name = name;
    database[name] = this;
    }

    So,

    object.name

    returns an object's name.

    database[name]

    returns the object.
    Douglas Crockford, Nov 5, 2004
    #3
  4. Lee wrote:

    > No. It would make no sense, since the same object can have many
    > different names. Also, if you think you need an object's name,
    > you are probably making a design mistake of some sort.


    Thank you!

    I'll try to describe the problem as a whole. I hope, somebody may offer
    the best decision.

    The class Button holds properties which describes a graphic button.
    Its method Button.Draw write the corresponding content:

    function Button_Draw()
    {
    document.write(
    "<div id=\"" + this.ID + "\"" +
    " onclick=\"Button_Click();\">"
    );
    ...

    Several buttons are grouping in the button bar:
    Bar's constructor:

    function Bar(buttons)
    {
    this.Items = new Array(arguments.length);
    for (i = 0; i < arguments.length; i++)
    this.Items = arguments;
    }

    In event handler Button_Click I need to get properties of Button object,
    which was creating this DIV element.

    If we have name of the Bar object, we may build the Button_Click calling
    in that way:

    "onclick=\"Button_Click('" + BarName + "', " + ButtonIndex + ");\""

    This is allow using in Button_Click the following code:

    function ButtonClick(BarName, ButtonIdx)
    {
    var t = eval(BarName).Items[ButtonIdx].Caption;
    ....
    }

    For this case I need to get object's name as string variable (BarName).

    I understand that this code looks ugly, but I do not see a way to make
    it better.

    Please, help!
    Oleg Alistratov, Nov 5, 2004
    #4
  5. Oleg Alistratov

    Matt Kruse Guest

    Oleg Alistratov wrote:
    > The class Button holds properties which describes a graphic button.
    > Its method Button.Draw write the corresponding content:
    > ... [and written-out content needs to refer back to its writer] ...


    What I do in some of my libs at the url below is to have a global array that
    stores references to objects of a certain type. Each time an object is
    created, it gets a unique index as one of its properties, and adds a
    reference to itself into the global array.

    So, any time it needs to create a stand-alone reference to itself, it can do
    something like
    document.write("GlboalObjectArray[" + this.index + "].doSomething()");

    There may be better ways to do it, but this works well for me.

    --
    Matt Kruse
    http://www.JavascriptToolbox.com
    Matt Kruse, Nov 5, 2004
    #5
  6. Oleg Alistratov

    Randy Webb Guest

    Oleg Alistratov wrote:


    > function ButtonClick(BarName, ButtonIdx)
    > {
    > var t = eval(BarName).Items[ButtonIdx].Caption;


    var t=window[BarName].......

    All Global variables are properties of the window object

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq
    Randy Webb, Nov 5, 2004
    #6
  7. Duncan Booth wrote:

    > Now all you have to do in Button_Click is use the ID to match the event to
    > the button:
    >
    > function Button_Click(theDiv) {
    >
    > var button = thebar.ButtonsById[theDiv.ID];

    ......................======
    > ...
    >
    > }


    I could found independently to such decision. The further experiments
    have appeared, because it was necessary to use multiple bars in the same
    document:

    var button = SomethingBig.BarById[theBar.ID].ButtonsById[theDiv.ID];

    I assumed to use

    eval(BarName)

    instead

    SomethingBig.BarById[theBar.ID]

    This situation has led to a problem described in the my first message.

    > BTW, you would be much better to manipulate the HTML DOM directly instead
    > of using document.write.


    Thanks, it's a good idea. This approach is new to me, I'll try to
    understand more in detail. Thank you!
    Oleg Alistratov, Nov 5, 2004
    #7
  8. Oleg Alistratov

    lawrence Guest

    Douglas Crockford <> wrote in message news:<1e7a7
    > Names can be useful in some applications. In those cases, keep the
    > objects in a little database.
    >
    > var database = {};
    > function MyObject(name) {
    > this.name = name;
    > database[name] = this;
    > }
    >
    > So,
    >
    > object.name
    >
    > returns an object's name.
    >
    > database[name]
    >
    > returns the object.


    Pardon the newbie question, but what does the word "this" mean here?
    It seems to have a different meaning in Java and PHP.
    lawrence, Nov 6, 2004
    #8
  9. Oleg Alistratov

    lawrence Guest

    Duncan Booth <> wrote in message
    > BTW, you would be much better to manipulate the HTML DOM directly instead
    > of using document.write. That would let you associate a function directly
    > with the event instead of having to use a text string. A function can
    > access variables in an outer scope when it was defined, so you can bypass
    > all of the id lookups and do something like:
    >
    > function AssociateButtonWithDiv(div, button) {
    > function clicked() {
    > button.clicked(div);
    > }
    >
    > if (isMozilla) {
    > div.addEventListener("click", clicked, false);
    > } else if (isIE) {
    > div.attachEvent("onclick", clicked);
    > }
    > }
    >
    > All code samples above are untested.


    Forgive the ignorant question, but what is isMozilla? Is that a
    constant that exists in Mozilla, to let you know that you're dealing
    with Mozilla? Where did you find it? Can you point me to some
    documentation regarding it?
    lawrence, Nov 6, 2004
    #9
  10. Oleg Alistratov

    Randy Webb Guest

    lawrence wrote:
    > Duncan Booth <> wrote in message
    >
    >>BTW, you would be much better to manipulate the HTML DOM directly instead
    >>of using document.write. That would let you associate a function directly
    >>with the event instead of having to use a text string. A function can
    >>access variables in an outer scope when it was defined, so you can bypass
    >>all of the id lookups and do something like:
    >>
    >>function AssociateButtonWithDiv(div, button) {
    >> function clicked() {
    >> button.clicked(div);
    >> }
    >>
    >> if (isMozilla) {
    >> div.addEventListener("click", clicked, false);
    >> } else if (isIE) {
    >> div.attachEvent("onclick", clicked);
    >> }
    >>}
    >>
    >>All code samples above are untested.

    >
    >
    > Forgive the ignorant question, but what is isMozilla? Is that a
    > constant that exists in Mozilla, to let you know that you're dealing
    > with Mozilla? Where did you find it? Can you point me to some
    > documentation regarding it?


    It falls in the same place the isIE variable falls. Its an utterly
    flawed attempt to detect the browser when the browser doesn't matter.
    The only way that isMozilla/isIE would be accurate in that scenario is
    if its defined like this:

    if(div.addEventListener) isMozilla = true;

    All of it is more predictable when written something like this:

    if (div.addEventListener) {
    div.addEventListener("click", clicked, false);
    } else if (div.attachEvent) {
    div.attachEvent("onclick", clicked);
    }

    Even then, div might not be a good variable name.


    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq
    Randy Webb, Nov 6, 2004
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Newton
    Replies:
    3
    Views:
    9,379
    Brock Allen
    Apr 4, 2005
  2. Mark Rae
    Replies:
    0
    Views:
    621
    Mark Rae
    Mar 7, 2006
  3. Antti S. Brax

    Name that anti-pattern

    Antti S. Brax, Sep 29, 2005, in forum: Java
    Replies:
    15
    Views:
    859
    Chris Smith
    Sep 30, 2005
  4. ding feng
    Replies:
    2
    Views:
    2,802
    ding feng
    Jun 25, 2003
  5. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    542
    Karl Seguin [MVP]
    Jun 16, 2006
Loading...

Share This Page