0

I was wondering whether there is any performance / advantage of writing javascript in this format ?

var myFuncs = {
  var firstFun = function() {
    // do something
  },

  var secondFunc = function() {
    // do something
  },

  var thirdFunc = function() {
    // do something
  }
}

So they can be called like

myFuncs.firstFun();

I'm trying to understand how this is more advantageous [other than code readability] ?

5
  • Advantage over what, making them global? Commented Feb 22, 2012 at 17:41
  • 1
    that is syntactically incorrect Commented Feb 22, 2012 at 17:41
  • Well making them like myFuncs.firstFun(); or just firstFun() Commented Feb 22, 2012 at 17:42
  • 2
    Your code will never run, so it will be very fast. Commented Feb 22, 2012 at 17:43
  • I'm trying to establish the advantage/disadvantage of putting functions within var myFuncs = {} as opposed to just using firstFun() ? Commented Feb 22, 2012 at 17:47

2 Answers 2

2

Function vs Object declaration

You can't use that particular syntax, the correct form is:

var myFuncs = {
  firstFn: function () {},
  secondFn: function () {},
  ...
};

The advantage to writing functions within an object has to do with namespacing and context. If you wrote:

var firstFn = function () {};
-or-
function firstFn() {}

the function would be defined at window.firstFn. Adding the functions on myFuncs makes the functions accessible at window.myFuncs.firstFn. If you want your JavaScript to work with other scripts you wouldn't want to have your foo function conflict with someone elses foo function:

<script src="a.js">
function foo() {...}
</script>

<script src="b.js">
function foo() {...} //this script would overwrite the foo function in a.js
</script>

<script src="c.js">
var bar = { //this script would be accessed at bar.foo()
  foo: function () {..}
}
</script>

The calling context (this) of the function will also be different:

function foo() {
  console.log(this); //window
}
var bar = {
  foo: function () {
    console.log(this); //bar object
  }
}

Closure Syntax

What you may be getting confused with is the syntax for declaring functions within a closure:

(function () {
  var foo = function () {...};
  foo();
}());

In this case the closure is used to prevent the function from polluting the global scope (window.foo will not be set). This allows multiple scripts to use the same function names without worrying about being overridden.


OOP Syntax

The object syntax is often used to define the prototype for a JavaScript "constructor". In JS all functions can be called as a constructor simply by using the new keyword when the function is called:

function foo() {...}
var f = new foo(); //don't do it this way

For readability/maintainability/consistency you should always name your constructor using PascalCase:

function Foo() {...} //tells other developers this is a constructor
function bar() {...} //tells other developers this is a function
var f = new Foo();
var b = bar();

Without getting too lost in the details of how prototype works, you can assign methods to be shared across every instantiated object of a function by assigning an object to the function's prototype property:

function Foo() {...}
Foo.prototype = { //note the use of the object declaration for the functions
  bar: function () {...},
  baz: function () {...},
  ...
};
var f = new Foo();
f.bar(); //calls the bar function that was defined in the prototype
Sign up to request clarification or add additional context in comments.

6 Comments

thanks. so in your opinion it is better "practice" to write within objects for my web application ? i.e. is better in the sense that it allows you to specifically declare myFuncs.firstFun(); which restricts the global scope - while probably making it easier to read ? i.e. account.settings(); etc ?
Start with writing your functions within a closure. If they're needed outside the closure, set them explicitly on the global object (i.e. window.foo = foo). Within the closure, if you have a need of Object Oriented JavaScript, declare some constructors and set the functions on the prototype.
just one last question. would this mean that the best practice would also be to place [i.e. using jQuery] - all your plugins in the same format ? i.e. var MyCompany = MyCompany || {}; MyCompany.Plugins = {}; and then use like MyCompany.Plugins.Blah() and MyCompany.Plugins.Ha() etc ?
jQuery plugins and widgets are typcially named jQuery.plugin.[version].js and would be expected to override $.fn.plugin and/or $.plugin. This naming convention makes it easy to know when plugins are going to conflict, you wouldn't include multiple jQuery.foo.js files, even if they did different things. If you're making a stand-alone lib for your company you might make lib.js and then lib.foo.js and then lib.bar.js, or you could simply use lib.js to include lib.foo and lib.bar but the assumption from the name would be that the script overrides window.lib.
but what if I want to call functions within the restricted scope so I can figure out what pages use what functions clearly ? that is like MyCompany.Account.Settings = function() { //some stuff; MyCompany.Plugins.Ha(); }; ?
|
0

I think what you're asking is for advantages between declaring all of your functions globally, vs putting them all into an object. There is no performance difference. The main reason why people generally suggest adding them all into an object is to not clutter up the global space.

Think about it this way. If you create a global function named init in a javascript library, and make it global. Then if someone else does the same, it will overwrite your init. But, if you put it in an object named someJSLibrary, the odds of some other code overwriting your functions are much less.

1 Comment

exactly :) cool thanks. so writing within objects is better in the sense that it allows you to specifically declare myFuncs.firstFun(); which restricts the global scope - while probably making it easier to read ? i.e. account.settings(); etc ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.