I tried running this code in firefox, and something appears to be looping…In the title area, to the left of the title, there’s a little wheel that just keeps spinning – like the page won’t completely load, maybe? The test I was doing was to try and see if I understand “instanceof”…
Thanks,
Jeff
!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<html>
<head>
<title>Instance test</title>
<script type = “text/javascript”>
var myObj = { star: "Algol", constellation: "Perseus" };
var var1 = "Algol";
if (var1 instanceof myObj) {
alert("Algol is an instance of myObj");
} else {
alert("Algol is not an instance of myObj");
}
</script>
<head>
<title>Instance test</title>
<script type=“text/javascript”>
// Bodies constructor
function Bodies(star,constellation)
{ this.star=star;
this.constellation=constellation;
}
// create instance of Bodies
var myObj = new Bodies(“Algol”, “Perseus”);
//
// check if is instance
if (myObj instanceof Bodies)
{ alert("Is an instance of Bodies with star= "+myObj.star); }
else
{ alert(“Is not an instance of Bodies”); }
//
</script>
</head>
As described in ECMAScript 262 3rd, The production: RelationalExpression instanceof ShiftExpression is evaluated as follows:
Evaluate RelationalExpression.
Call GetValue(Result(1)).
Evaluate ShiftExpression.
Call GetValue(Result(3)).
If Result(4) is not an object, throw a TypeError exception.
If Result(4) does not have a [[HasInstance]] method, throw a TypeError exception.
Call the [[HasInstance]] method of Result(4) with parameter Result(2).
Return Result(7).
The sixth step above points out: the ShiftExpression should have a [[HasInstance]] method which is only implemented by Function Objects among native ECMAScript objects.
In your demo, myObj is not a Function Object, so error will be thrown.