-1

I have a javascript file with a lot of global objects. That is, I have a lot of the following sorts of code going on:

var foo = 5;
var bar = "string";

var getStuff = function () {
    //...getStuff
}

Question: What is the easiest way for me to place the entirety of this file into an object?

It seems to me my options are as follows:

  1. Wrap the entire file in a var Object = { ... };. The problem is that is that I have to change the syntax of how variables and functions are declared.

  2. Manually prefix Object.xxx before each declaration. This is also a lot of manual work, and makes things slightly verbose.

Is there a better way? Or if not, is there a reason to prefer (1) or (2)?

George
  • 5,909
  • 2
  • 24
  • 53
  • That's not a class. It is an object. JavaScript doesn't have real classes. Also, what you are looking for is an [immediately invoked function expression](http://en.wikipedia.org/wiki/Immediately-invoked_function_expression). – Aadit M Shah Apr 07 '15 at 17:30
  • You're right -- I changed the terminology from "class" to "object". – George Apr 07 '15 at 17:31
  • 1
    Going with the [IIFE](http://stackoverflow.com/questions/8228281/what-is-the-function-construct-in-javascript) Aadit mentioned, you can try a [revealing module](http://stackoverflow.com/questions/5647258/how-to-use-revealing-module-pattern-in-javascript). – Jonathan Lonowski Apr 07 '15 at 17:32

3 Answers3

1

I guess you want this way

var someObject = function (window, document, undefined) {
  /*you code...etc here*/
  return {
    method1 : your_method...,
    method2 : your_method...,
    method2 : your_method...,
    object : your_object...
  }
} (window, document);

The reason to pass window and document here is to make it be a local variable instead a foreign variable to minimize the cost for lookup variable. Most js engine now can handle that by themself, so this may not be really required nowadays. Just in case this code may be ran on a really old browser.

And the undefined passed that way is also due to compatibility reason, undefined is a override variable on some old browsers. New browsers won't allow such action.

Jerry
  • 818
  • 7
  • 12
1

You're not looking for a Class or Object. Looking at your description, I think you want something like a module.

You can hide your variables from global scope and "export" relevant bits like this:

var MyModule = (function(){

  // regular code here
  var hidden = 10,
      visible = 20;

  function doSomething() {
    return hidden + visible;
  }

  // the "export" part
  return {
    twenty: visible,
    doSomething: doSomething
  };

})(); 

// then:

MyModule.doSomething(); // => 30
MyModule.twenty + 1 // => 21
MyModule.hidden     // undefined - no such thing here 
// hidden, visible and doSomething without MyModule. are invisible here

That's one example of emulating modules in JS. That (function(){...})() part is called IIFE.

nooga
  • 530
  • 1
  • 6
  • 19
1

There are different JavaScript design patterns you can follow to accomplish this.

I suggest going over this online book: http://addyosmani.com/resources/essentialjsdesignpatterns/book/

andybeli
  • 746
  • 5
  • 13