mardi 3 novembre 2015

which is the best way to declare functions in module pattern in Javascript [duplicate]

This question already has an answer here:

I am confusing about the declaring functions in module pattern. can anyone please tell me which is the best way to declare the function inside the module from these below two options.

Method: 1

var nameSpace = (function(){
  var privateVariableOne;
  var privateVariableTwo;

  var publicMethodOne = function(){

  };
   var publicMethodTwo = function(){

  };
  return {
      publicMethodOne: publicMethodOne,
      publicMethodTwo: publicMethodTwo 
  };
}());

Method: 2

var nameSpace = (function(){
  var privateVariableOne;
  var privateVariableTwo;

  function publicMethodOne(){

  };
 function publicMethodTwo (){

  };
  return {
      publicMethodOne: publicMethodOne,
      publicMethodTwo: publicMethodTwo 
  };
}());

I think second one creates memory leaks as it is a closure of parent function. Please give me clarification on this.

Aucun commentaire:

Enregistrer un commentaire