0

I have seen developers using different ways to declare/define functions in js,

like:

// 1
createview:function()
{

}

// 2.
var createview=function()
{
}

// 3.
function createview()
{
}

While 2nd and 3rd are function expression and declaration respectively, what's with the 1st usage?

matisetorm
  • 847
  • 8
  • 21
Prashant K
  • 690
  • 7
  • 7
  • the first case is used when you want to assign function as property of object – StateLess May 15 '14 at 05:45
  • C'mon guys. This is not duplicate. Without other object notation the answer should be `label` in Javascript. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label – Herrington Darkholme May 15 '14 at 05:51
  • 1
    @HerringtonDarkholme That's not correct. An anonymous function cannot be used as a statement (and it would serve no purpose if it could). Pretty sure OP just left the relevant details out. – JLRishe May 15 '14 at 06:35

1 Answers1

0

Its an object and used for example in the revealing module pattern.

var module = (function() {

  var stuff = function() { return 'stuff'; };

  return {

    stuff: stuff

  }

}());

Now you have a name spaced closure.

module.stuff() //--> returns 'stuff'
Sten Muchow
  • 6,373
  • 4
  • 33
  • 46