471,075 Members | 856 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 software developers and data experts.

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.
Jul 20 '05 #1
7 3993
VK
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 <ym*@kicon.com> wrote in message
news:4c**************************@posting.google.c om...
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.

Jul 20 '05 #2
ym*@kicon.com (chirs) writes:
These 2 lines caused an error in IE5. The error is "Function
expected". Why? var d=new Date();
document.write(d instanceof Object + "<br>");


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 - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
Lee
chirs said:

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>");


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

Jul 20 '05 #4
"VK" <sc**********@yahoo.com> wrote in message
news:3f***********************@news.freenet.de...
Because the last line has no sense.
That is the sort of statement that explains why Usenet quoting
conventions are as they are.
Besides 'instanceof' and 'object' are reserved (but not
implemented yet) words in JavaScript, you have to quote
them (or better not use at all).
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 RelationalExpression: RelationalExpression
instanceof ShiftExpression is evaluated as follows:
1. Evaluate RelationalExpression.
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.
What output are you trying to get?

<snip>

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

Richard.
Jul 20 '05 #5
Hi,

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

d instanceof Object

should result true.
Jul 20 '05 #6
> You're making a bad assumption about operator precedence.
Try:
document.write((d instanceof Object) + "<br>");

Thank you. But the book says instanceof has higher precedence than +.
Jul 20 '05 #7
Lee
chirs said:
You're making a bad assumption about operator precedence.
Try:
document.write((d instanceof Object) + "<br>");

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


Your book is wrong.
Most technical books contain mistakes.

Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Beginner | last post: by
4 posts views Thread by Sean Inglis | last post: by
2 posts views Thread by System Administrator | last post: by
15 posts views Thread by shana07 | last post: by
13 posts views Thread by Robert | last post: by
3 posts views Thread by whitelined | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.