Wednesday, June 12, 2013

Javascript - namespace/module example


Method1: (This is what I found in an example)
--------
var SILLY = (function (module) {
      
      module.DoIt = function (resultObject) {
          resultObject.prepend(Date() + '
'); }; return module; } (SILLY || {}); // "{}" means "new Object()". It is a shortcut NOTE: The last line could be : (SILLY || {}) => (SILLY || (new Object()))); ----- Method2: (This is how we would write the above code in C#/Java style) --------- var SILLY = (function() { if (SILLY === undefined) { var SILLY = new Object(); SILLY.DoIt = function (resultObject) { resultObject.prepend(Date() + '<br >'); }; } return SILLY; }());
jsbin link for full example - http://jsbin.com/ahuqow/1/edit

No comments:

Post a Comment

Followers

Blog Archive