Spidermonkey JS question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • I?igo Koch

    Spidermonkey JS question

    Hi,

    I need to create an object that at the same time has a string value:

    foo = "string1";
    foo.bar = "string2";

    It must be possible since browsers have a lot of these cases:
    location = "str";
    location.href = "str";
    etc..

    Thank you in advance for any light on this problem,

    regards,

    Iñigo Koch
  • Lee

    #2
    Re: Spidermonkey JS question

    I?igo Koch said:[color=blue]
    >
    >Hi,
    >
    >I need to create an object that at the same time has a string value:
    >
    >foo = "string1";
    >foo.bar = "string2";
    >
    >It must be possible since browsers have a lot of these cases:
    >location = "str";
    >location.hre f = "str";
    >etc..
    >
    >Thank you in advance for any light on this problem,[/color]

    DOM objects have the advantage of being compiled into the
    browser, so they can do some magic that the rest of us can't.

    You can create your own toString() method for your object,
    that can make it appear to have a different string value
    than has been assigned to it. Maybe if you explain what it
    is that you're trying to do, somebody can suggest a way to
    do it.

    Comment

    • Brendan Eich

      #3
      Re: Spidermonkey JS question

      I?igo Koch wrote:
      [color=blue]
      >Hi,
      >
      >I need to create an object that at the same time has a string value:
      >
      >foo = "string1";
      >foo.bar = "string2";
      >
      >It must be possible since browsers have a lot of these cases:
      >location = "str";
      >location.hre f = "str";
      >etc..
      >
      >[/color]

      Mozilla uses a setter for window.location that can turn the string into
      a URL, and internally do the equivalent of 'location.href = string'.
      You can define a setter for an individual property when you define the
      property, using JS_Define*Prope rty. You can also define a class
      setProperty hook that special-cases based on id.

      /be
      [color=blue]
      >Thank you in advance for any light on this problem,
      >
      >regards,
      >
      >Iñigo Koch
      >
      >[/color]

      Comment

      • Georg Maaß

        #4
        Re: Spidermonkey JS question

        I?igo Koch wrote:[color=blue]
        > Hi,
        >
        > I need to create an object that at the same time has a string value:
        >
        > foo = "string1";
        > foo.bar = "string2";
        >
        > It must be possible since browsers have a lot of these cases:
        > location = "str";
        > location.href = "str";
        > etc..[/color]

        Only the first sample contains an implicite cast; the second does not.
        The second sample uses a setter as written by Brendan. The first sample
        first assigns a string value to the variable foo. foo now contains a
        string value, not an object. foo.bar = "string2"; now casts foo
        temporary to a String wrapper object. This is caused by the element
        lookup operator. The string value "string2" is assigned to the bar
        property of the temporary string object.

        If you output foo you get "string1"; if you ouptut foo.bar, you get
        undefined or nothing.

        alert(foo + '\n' + foo.bar);

        this alerts:

        string1
        undefined


        js> print(foo + '\n' + foo.bar);

        this writes:

        string1



        --
        Georg Maaß - bioshop.de D-76227 Karlsruhe, Westmarkstraße 82
        HTML, XML / JavaScript, C++, Java, PHP, VB / CGI, JSP, ASP, ASP.net
        - The ultimative DHTML engine: http://gml-modul.sourceforge.net -
        Download gmL-Modul for free. JavaScript1.2 based DHTML tool, which does it\'s dynamic browser optimisations on server side via PHP or C++. Code generation and size may be controlled by many parameters including debug features.


        Comment

        Working...