instanceof causes an error in IE5

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chirs

    instanceof causes an error in IE5

    Hi,

    These 2 lines caused an error in IE5. The error is "Function expected". Why?


    var d=new Date();
    document.write( d instanceof Object + "<br>");

    Thanks.
  • VK

    #2
    Re: instanceof causes an error in IE5

    Because the last line has no sense.
    Besides 'instanceof' and 'object' are reserved (but not implemented yet)
    words in JavaScript, you have to quote them (or better not use at all).

    What output are you trying to get?

    document.write( d+" instanceof Object") gives you
    "Fri Oct 3 23:07:55 UTC+0200 2003 instanceof Object"
    because toString() method is automatically called for d which gives a UTC
    string
    and what a hey does it suppose to mean ?

    If you wanted to check the type of the instance (just a wild guess), then:
    document.write( "d is "+typeof(d) ) gives you
    "d is object"






    chirs <yma@kicon.co m> wrote in message
    news:4c22a744.0 310031249.2afd4 962@posting.goo gle.com...[color=blue]
    > Hi,
    >
    > These 2 lines caused an error in IE5. The error is "Function expected".[/color]
    Why?[color=blue]
    >
    >
    > var d=new Date();
    > document.write( d instanceof Object + "<br>");
    >
    > Thanks.[/color]


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: instanceof causes an error in IE5

      yma@kicon.com (chirs) writes:
      [color=blue]
      > These 2 lines caused an error in IE5. The error is "Function
      > expected". Why?[/color]
      [color=blue]
      > var d=new Date();
      > document.write( d instanceof Object + "<br>");[/color]

      It gives an error in Opera 7 too. Since Opera isn't IE, you can
      actually use the error message:

      Statement on line 2: Second argument to 'instanceof' is not an
      Object: Object + "<br>"

      So, the problem is the association. It first adds Object and "<br>",
      giving a string, and then tries to see whether d is an instance of
      that string. And you can't be an instance of a string, only of a
      function, which is why IE expected a function..

      Add brackets:
      document.write( (d instanceof Object) + "<br>");

      Then it works in Opera 7, and it writes "true<br>" to the document.

      (and "instanceof " has been in JScript since version 5 and Javascript
      since ersion 1.4, and is a part of ECMAScript, and Object is a function
      in Javascript since version 1.0).

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Lee

        #4
        Re: instanceof causes an error in IE5

        chirs said:[color=blue]
        >
        >Hi,
        >
        >These 2 lines caused an error in IE5. The error is "Function expected". Why?
        >
        >
        >var d=new Date();
        >document.write (d instanceof Object + "<br>");[/color]

        You're making a bad assumption about operator precedence.
        Try:
        document.write( (d instanceof Object) + "<br>");

        Comment

        • Richard Cornford

          #5
          Re: instanceof causes an error in IE5

          "VK" <schools_ring@y ahoo.com> wrote in message
          news:3f7dea0b$0 $15706$9b622d9e @news.freenet.d e...[color=blue]
          > Because the last line has no sense.[/color]

          That is the sort of statement that explains why Usenet quoting
          conventions are as they are.
          [color=blue]
          >Besides 'instanceof' and 'object' are reserved (but not
          >implemented yet) words in JavaScript, you have to quote
          >them (or better not use at all).[/color]

          The Instanceof operator was implemented in JavaScript 1.4 and JScript
          5.0 and is specified in ECMA 262 3rd edition (section 11.8.6):-

          <quote>
          11.8.6 The instanceof operator
          The production RelationalExpre ssion: RelationalExpre ssion
          instanceof ShiftExpression is evaluated as follows:
          1. Evaluate RelationalExpre ssion.
          2. Call GetValue(Result (1)).
          3. Evaluate ShiftExpression .
          4. Call GetValue(Result (3)).
          5. If Result(4) is not an object, throw a TypeError exception.
          6. If Result(4) does not have a [[HasInstance]] method, throw
          a TypeError exception.
          7. Call the [[HasInstance]] method of Result(4) with parameter
          Result(2).
          8. Return Result(7).
          </quote>

          The identifier - Object - (initial capital) is a reference to the global
          Object constructor and will be resolved without error in all ECMA Script
          implementations .

          The expression - d instanceof Object - is valid in all recent ECMA
          Script implementations and should return a boolean value. The OP may be
          suffering from another instance of the built in objects (such as Date)
          not quite complying with the ECMA specs in JScript.
          [color=blue]
          >What output are you trying to get?[/color]
          <snip>

          The expression provided as the document.write parameter should resolve
          to the strings "true<br>" or "false<br>" .

          Richard.


          Comment

          • chirs

            #6
            Re: instanceof causes an error in IE5

            Hi,

            I am reading a book JavaScript The Definitive Guide. On P67, it says that

            d instanceof Object

            should result true.

            Comment

            • chirs

              #7
              Re: instanceof causes an error in IE5

              > You're making a bad assumption about operator precedence.[color=blue]
              > Try:
              > document.write( (d instanceof Object) + "<br>");[/color]


              Thank you. But the book says instanceof has higher precedence than +.

              Comment

              • Lee

                #8
                Re: instanceof causes an error in IE5

                chirs said:[color=blue]
                >[color=green]
                >> You're making a bad assumption about operator precedence.
                >> Try:
                >> document.write( (d instanceof Object) + "<br>");[/color]
                >
                >
                >Thank you. But the book says instanceof has higher precedence than +.[/color]

                Your book is wrong.
                Most technical books contain mistakes.

                Comment

                Working...