SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2010
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    is this in a loop?

    Hello;

    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>
    <body>
    <p>Chapter 5 Example</p>
    </body>

  2. #2
    SitePoint Addict AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You had a few problems with your code. This one works.

    Code :
    <!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">
    // 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>
     
    <body>
     
    <p>Chapter 5 Example</p>
     
    </body>
     
    </html>

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2010
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks AllanP

  4. #4
    SitePoint Member
    Join Date
    Sep 2010
    Location
    Shanghai, China
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jefals View Post
    Hello;

    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>
    <body>
    <p>Chapter 5 Example</p>
    </body>
    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.

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2010
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Goddy128

  6. #6
    SitePoint Member
    Join Date
    Sep 2010
    Location
    Shanghai, China
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jefals View Post
    Thanks Goddy128
    You are welcome!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •