2 Replies Latest reply on Jun 25, 2007 8:18 PM by McFazstp

    Javascript Symbols Not Equal

    McFazstp Level 1
      If I have two identical symbols in Javascript they are not equal as they would be in Lingo

      var a = symbol( 'hello' );
      var b = symbol( 'hello' );
      put ( a == b );
      // false
      put ( a.toString() == b.toString() );
      // true

      -- Lingo
      a = #hello
      b = #hello
      put a = b
      -- 1

      So my question is is this a bug or just the way Javascript works?
        • 1. Re: Javascript Symbols Not Equal
          Ex Malterra Level 1
          i suspect the reason for inequality is that the two symbols are serate objects, each with it's own memory address. try this:
          you'll get:
          // object
          toString type casts the symbol to it's string value.

          this from the srcipting reference pdf:

          A symbol is a string or other value in Lingo that begins with the pound (#) sign.
          Symbols are user-defined constants. Comparisons using symbols can usually be performed very
          quickly, providing more efficient code.
          For example, the first statement below runs more quickly than the second statement:
          -- Lingo syntax
          userLevel = #novice
          userLevel = "novice"
          Symbols cannot contain spaces or punctuation.
          In both Lingo and JavaScript syntax, convert a string to a symbol by using the symbol() method.
          -- Lingo syntax
          x = symbol("novice") -- results in #novice
          // JavaScript syntax
          var x = symbol("novice"); // results in #novice
          Convert a symbol back to a string by using the string() function (Lingo) or the toString()
          method (JavaScript syntax).
          -- Lingo syntax
          x = string(#novice) -- results in "novice"
          // JavaScript syntax
          var x = symbol("novice").toString(); // results in "novice"
          In JavaScript syntax, you cannot compare symbols of the same name to determine whether they
          refer to the same symbol. To compare symbols of the same name, you must first convert them to
          strings by using the toString() method, and then perform the comparison.
          • 2. Re: Javascript Symbols Not Equal
            McFazstp Level 1
            What do you know it's in the help file... thanks for that.