Loading ...
Sorry, an error occurred while loading the content.

Re: [jslint] server js / browser js compatible module pattern? (UNCLASSIFIED)

Expand Messages
  • Cheney, Edward A SSG RES USAR USARC
    Classification: UNCLASSIFIED AJ, ... Use the CommonJS framework to achieve multiple environment compatibility. However, this will not work if your code is
    Message 1 of 1 , Feb 20, 2011
    • 0 Attachment
      Classification: UNCLASSIFIED
      AJ,

      > I'm trying to find a method that will work for both ssjs and bsjs modules
      > and pass JSLint

      Use the CommonJS framework to achieve multiple environment compatibility. However, this will not work if your code is reliant upon any particular environment, such as a DOM interface. Fortunately, Mr. Crockford had the presence of mind to build JSLint independent of the DOM and then attach an interface for the web tool separately.

      CommonJS works by reading the entire contents of a given JS file and writing output into a global function named 'exports'. You access this output by using a different global function called 'require'.

      Here is the use of 'exports' from my own application:
      if (typeof exports !== "string") {
      exports.api = function (x) {
      "use strict";
      return prettydiff(x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9], x[10], x[11], x[12], x[13], x[14], x[15], x[16], x[16], x[17], x[18]);
      };
      }

      In order to further isolate my application from any single environment I set a global variable named 'exports' to an empty string in my DOM interface, so that if the DOM is available than CommonJS will not be accessed and no errors are thrown for 'undefined'.

      Here is my use of the 'require' function:
      var daemon = require('daemon'),
      http = require('http'),
      mailer = require('mailer'),
      prettydiff = require('/home2/mailmark/public_html/prettydiff/prettydiff');

      I hope this helpful.

      Austin Cheney, CISSP
      http://prettydiff.com/
      Classification: UNCLASSIFIED
    Your message has been successfully submitted and would be delivered to recipients shortly.