473,383 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 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 4112
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John MacIntyre | last post by:
Hi, I have a page with a series of child pages loaded into an iframe. When I move from page to page, I store an object containing the child's control data in a variable on the main page, then...
5
by: Beginner | last post by:
What is the c++ version of instanceof()? Thank-you
4
by: Sean Inglis | last post by:
Well bizarre to me, anyway. I've distilled it down to two small files: testtop.htm =============================== <html> <head> <script language="Javascript">
2
by: System Administrator | last post by:
Hi: Can someone explain why BOTH of the following are true ? Function instanceof Object //true Object instanceof Function //true (Either one or the other should be true, not both,...
2
by: System Administrator | last post by:
function a() { } typeof a //returns 'function' a instanceof a //returns false typeof Object //returns 'function' Object instanceof Object //returns ...
15
by: shana07 | last post by:
Can I have many instanceof code for one 'if' ? for example..... if(checkMe instanceof AInstruction instanceof LocVariableInst) What I am trying to say is, these instanceof are refer to these...
13
by: Robert | last post by:
Hi, Is it possible to intercept a call to "instanceof" and return something different? I wanted to see if it was possible to implement something like multiple inheritance, especially to use...
30
by: kj | last post by:
My book (Flanagan's JavaScript: The Definitive Guide, 5th ed.) implies on page 111 that the following two constructs are equivalent: ( x.constructor == Foo ) and ( x instanceof Foo ) The...
3
by: whitelined | last post by:
Hi, How widely supported is the instanceof operator? Is there an alternative to seeing if an object is an instance of a constructor? Many thanks Regards Aaron
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.