News for hackers. Stuff that matters.
Re:“var functionName” vs “function functionName” by reconor on Wednesday April 30, 2014 @01:09PM (#7674660)
Using var to declare a variable later assigned to an anonymous function can help with managing larger JavaScript projects where scoping and data member access is important. Here is a pattern I tend to apply rather frequently, included is an example for a custom instance object, which would be created with 'new', and a Namespace object:

    var MyObject;
    (function() {
      var staticPrivateVariable;

      MyObject = function(opts) {
        // Do Initialization Stuff
      };

      MyObject.prototype.publicInstanceFunction = function(opts) {
        // Do instance stuff
      };
    })();

    
    var Namespace;
    (function() {
      var staticPrivateVariable;

      Namespace = function(opts) {
        // Do Initialization Stuff
      };
      Namespace.publicFunction = function(opts) {

      };

      function privateFunction(opts) { 
        // Do some private stuff
      }
    })();

There are no comments on this comment yet.