title: How to define a new global function in javascript
body: <p>I have an issue trying to make a function global when it is involved in closure.  In the code listed below I have an anonymous method which defines at new function on the <code>window</code> called, <code>getNameField</code>.</p>
  
  <pre><code>(function () {
      function alertError (msg) {
          alert(msg);
      }
      window.getNameField = function (fieldId) {
          try{
              if(!fieldId) {
                  fieldId='name';
              }
              return document.getElementById(fieldId);
          } catch(e) {
              alertError(e);
          }
      };
  }());
  
  alert(getNameField().value);
  </code></pre>
  
  <p>This works great in the browser, but when I run the code in <a href="http://jslint.com" rel="noreferrer">JSLint.com</a> with "Disallow undefined variables" turned on it gives me an error.</p>
  
  <blockquote>
    <p>Problem at line 17 character 7:
    '<code>getNameField</code>' is not defined.</p>
  </blockquote>
  
  <p>Can you help me fix this so that JSLint actually understands that this function should be considered global?</p>