SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Passing document As An Argument

    Quite often, I define variables like so:

    Code:
    var something = (function () {
        if (x) {
            ...
        } else {
            ...
        }
    }());
    A good deal of the time, I'll use document inside that closure. Like, document.getElementById for example. Then, when I run it through JSLint, I get a bunch of errors that "document" hasn't been defined. So then I do this:

    Code:
    var something = (function (document) {
        if (x) {
            ...
        } else {
            ...
        }
    }(document));
    That makes it so that the error only shows up one time in JSLint, which makes me feel good. This is solely to satisfy my own personal tics. I know I shouldn't code exactly according to what JSLint says, I know that it's only a guide, blah blah blah.

    My question is, are there any performance issues with doing this? With passing the document object to an anonymous function that's only being executed once, immediately?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    You don't need to make use of that work-around.

    When your code is intended to run in a web browser, the document object will always be available to you.

    JSLint allows you to specify that assumption too, by providing a checkbox on the JSLint page called "Assume a browser"
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •