Is this in a loop?


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”…


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


<p>Chapter 5 Example</p>

You had a few problems with your code. This one works.

<!doctype HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “”>

<title>Instance test</title>
<script type=“text/javascript”>
// Bodies constructor
function Bodies(star,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= "; }
{ alert(“Is not an instance of Bodies”); }


<p>Chapter 5 Example</p>



Thanks AllanP

As described in ECMAScript 262 3rd, The production: 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).
    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.

Thanks Goddy128

You are welcome!:slight_smile: