0
var mainObject = function(){
   var constNine = 9;
   var a = [1,2,3];
   var key = 'abc';
   function publicFunction(){
     var x = 5;
     return (x*a[0]*a[1]*constNine)+a[2];
   }

   function privateFunction()
   {
     var x = 5;
     return (x+a[0]+a[1])*a[2];
   }

   return {
      key: key,
      publicFunction: publicFunction
   }
}();

Here in the above code:

  1. mainObject is an object and is in window scope i.e. can be accessed from outside js as well.
  2. 'constNine' is integer value, not accessible in the window scope, can be used inside the mainObject only. It is a constant value 9.
  3. 'a' is an array and is not accessible in the window scope, can be used inside the mainObject only.
  4. 'key' is a string var and is in window scope.
  5. 'publicFunction' is a function and it is in window scope.
  6. 'x' is integer variable and is only accessible within publicFunction
  7. 'privateFunction' is a function and is not accessible in the window scope, can be used inside the mainObject only.

As javascript code grows, it would be really useful to put naming convention for all these JS variables and objects. Could someone suggest how should these be named i.e. should have underscore at start, should be camel case, should be all capital etc.?

I read several guides and questions like this but didn't get answer that covers all the above points.

Sahil Sharma
  • 3,847
  • 6
  • 48
  • 98
  • 1
    `const` is a keyword of ES6. – Nina Scholz Sep 10 '17 at 20:16
  • editted the question, renamed it – Sahil Sharma Sep 10 '17 at 20:17
  • How you chose to name your variables, would be opinion-based – adeneo Sep 10 '17 at 20:18
  • yeah i agree.. but is there any document that specify convention for all above cases? i.e. which should i actually distinguish by writing them differently and which I should name is same manner – Sahil Sharma Sep 10 '17 at 20:19
  • As it was said, it's opinion based. And 'scope' term is used incorrectly here. *'key' is a string var and is in window scope* - it certainly isn't. Generally you don't want to encapsulate members inside IIFE, because this kind of encapsulation smells and doesn't allow reflection. This is ES6 code, and in ES6 there are classes that set a standard for OOP. There's a convention to provide `_` suffix for object properties that are considered private. It can be followed or not. – Estus Flask Sep 10 '17 at 20:50
  • @estus "Generally you don't want to encapsulate members inside IIFE" then how would I declare variable that can be used by all functions inside IIFE objects? – Sahil Sharma Sep 11 '17 at 05:08
  • Declare them as object props and refer it as `this`. You don't even need IIFE there. It's `const obj = { _privateMethod: ... , publicMethod: ... }. This improves testability greatly. – Estus Flask Sep 11 '17 at 08:02
  • I am writing with es5 – Sahil Sharma Sep 11 '17 at 08:38

1 Answers1

0

Looking at your example specifically:

  • mainObject should be const (assuming you can use ES6, which is a safe bet).
  • The first three variable should also be defined as const, though why you would want to store '9' as a variable that you use once I'm not sure...? Similarly for abc?
  • As you use x twice, I would define this with let, to avoid confusion.

Since ES6 there isn't really any reason to use var - const can be used for variable that won't change (and indeed can't change), whereas let gives the variable scope only within its block (i.e. within a function, or logic block, like an if statement/for loop), and allows its value to be changed.

But there are other weird things going on here. Why would you define a 'private function' that's not meant to be called elsewhere, when you don't use it inside the mainObject? And if this is not a function that takes arguments, why not just define the return object as its own const?