Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacement for instanceof operator

Status
Not open for further replies.
Joined
Dec 8, 2003
Messages
17,047
Location
GB
I'm trying to modify a piece of code that uses the instanceof operator so that it works on IE 5/Mac. With the instanceof operator in place, the code simply fails to do anything on the Mac. No errors are thrown, but the code simply fails to load (apparently, at least... an alert statement right at the top of the file isn't called, even though these lines are right near the bottom):

Code:
if (obj instanceof Date) {
	// do something with obj here
} else if (obj instanceof Array) {
	// do something with obj here
}

The replacement code I've come up with does at least run:

Code:
if (obj.constuctor == new Date().constructor) {
	// do something with obj here
} else if (obj.constuctor == new Array().constructor) {
	// do something with obj here
}

But I'm wondering if it will do the same job.

Does anyone know if the two pieces of code are technically the same? Would there be any situations where one works but the other does not?

Thanks!

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Well, Dan, that's what you get for being a JavaScript guru. The only questions you have are way above the heads of the rest of us!

If this were a different question, posed in the same way, my initial response would be "test it and tell us what you find out." I imagine you've done this, however.

So, what did you find out?

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
Well - my initial tests (pre-post) showed that they did the same thing... but it just feels like the answer shouldn't be that simple. It feels like there is something nagging away at me saying that there may be an example where one gives a different result from the other - I just can't think of it.

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top