instanceof causes an error in IE5

C

chirs

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.
 
V

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"
 
L

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

Lasse Reichstein Nielsen

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
 
R

Richard Cornford

VK said:
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.
 
C

chirs

Hi,

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

d instanceof Object

should result true.
 
C

chirs

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 +.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,102
Messages
2,570,645
Members
47,245
Latest member
ShannonEat

Latest Threads

Top