Decoding jQuery – $.isWindow: Detecting window object

In the Decoding jQuery series, we will break down every single method in jQuery, to study the beauty of the framework, as an appreciation to the collective/creative geniuses behind it.

jQuery.isWindow()
jQuery has a jQuery.isWindow() method, this is used in a number of places in jQuery itself to determine if it’s operating against a browser window (such as the current window or an iframe).

<!doctype html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  Is 'window' a window? <b></b>
<script>$("b").append( "" + $.isWindow(window) );</script>
 
</body>
</html>

So how does the actual source look like? It looks like below:

// A crude way of determining if an object is a window
isWindow: function( obj ) {
  return obj && typeof obj === "object" && "setInterval" in obj;
}

Here is how it works:
1. it detects if there is an object being returned
2. it detects if the object type is object
3. it sees if setInterval is a method of the object, setInterval is part of window.setInterval, it’s a method that’s only available in window object.

Reference:
http://api.jquery.com/jQuery.isWindow/
https://github.com/jquery/jquery/blob/master/src/core.js#L480
https://developer.mozilla.org/En/Window.setInterval

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Technorati
  • Twitter
  • Yahoo! Bookmarks

About Shi Chuan

I am a web developer.
This entry was posted in JavaScript and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">