views:

148

answers:

6

What is the 'best practise' with regard to coding style.

Should I use _ for private members? Should I use this._privateMember?

Please re-write my code in proper style if its wrong:

(function()){

   var _blah = 1;


   someFunction = function() {

  alert(_blah);

   };

   someOtherFunction = function {

  someFunction();

   }


}();
+1  A: 

I don't think there is one. Use a prefix if you think it helps.

I use _ for private members because it distinguishes them which can be quite helpful in Javascript when you have variables coming from all over the place. But they do clutter the code a bit.

Skurmedel
+1  A: 

I don't use _ for variables that are declared using var. I do however, use _ to denote object members that shouldn't be access directly.

Some people (who are strange in my opinion :P), also prefix variables with a $ if it contains a jQuery object.

As long as you are consistent with what you do, there are no problems.

Aside from just the code you're showing now, you should use Capital Letters to distinguish constructor functions, and camelCase to name instances of objects.

function Foo (val) {
  this.set(val);
};

Foo.prototype.get = function () {
  return this._dontTouchMePlease;
};

Foo.prototype.set = function(val) {
  this._dontTouchMePlease = parseInt(val, 10);
};

var aFoo = new Foo(6);
Matt
A: 

I also use the "_" in c# for private/protected members. It is a fast way to see if the variable is a member-variable or not. And you can access it faster with code-completion because you don't get in mess with the public members (private: _blah , public property: Blah).

But are there any private members in javascript anyway? I think every variable defined as member is accessible from the outside. So you don't have a public wrapper for the private member. And that means the "_" is a bit a overhead and the same can be achieved with "this.".

cpt.oneeye
You cannot create private variables when using the prototype pattern to create objects, so this is where I use `_` to denote members I wish to be private; but yes, you're right, they are still perfectly accessible from the outside.
Matt
+4  A: 

I would read this and incorporate any part you agree with:
http://javascript.crockford.com/code.html

You do not have to agree with all of it

Christopher Altman
+1  A: 

I think that its generally accepted that if a variable name starts with a _, you probably shouldn't touch it (accept in dire cirumcstances and even then, two keys and special codes should be provided).
If I'm remembering my Crockford correctly, you'll want to put var in front of the two inner functions, otherwise they will be implicit globals. If you want them to be globals, then that's moot. Either way, your second inner function declaration should probably end in a semicolon.
This might be a misformating thing, but I think its generally accepted that the bodies of functions are indented one more level in. Also, I've never seen the (function()){/* stuff */}(); construction before, but that says pretty much nothing.
I'd write it these ways - one for if your just declaring a function and another for if your using an anonymous function and immediately applying it to get a result, because I don't which one you're trying to do (again, if you want the inner functions to be global, then this won't be what you intended):

//function declaration
var myFunction = function () {
  var _blah = 1;
  var someFunction () {
    alert(_blah); //or console.log(_blah); for debugging purposes
  };
  var someOtherFunction () {
    someFunction();
  };
};
//using a one-of to assign a result
/* NOTE: if you are using this version, myResult will be undefined 
   (at least given the functions you provided), but like I said, 
   I don't recognize the construction you provided, and am therefore
   assuming that you meant one of these two, which could be a perfectly 
   falacious assumption, and in that case, my apologies
*/
var myResult = function () {
  var _blah = 1;
  var someFunction () {
    alert(_blah);
  };
  var someOtherFunction () {
    someFunction();
  };
}();

BTW, (and I don't want to overstep) Crockford's "JavaScript: The Good Parts" is a stellar reference for JavaScript syntax. He also has, on his website a JavaScript style guide of sorts (though I don't know how widely followed it is). Link is: http://javascript.crockford.com/code.html

yarmiganosca
A: 

Hi,

I prefer you to use the following stuffs which is preferably used around the world programmers.. see below

i  = Int
f  = Float
o   = Object
r   = Return
a   = Array
e   = Element
g   = Global declaration
hook    = a function which can be used for hooking with other functions
call    = a function which can be used for making call from client to server system
sync    = a function which can be used for SYNC

and so on.. you can prefix on your coding...

VAC-Prabhu