Javascript best practices for variables and functions - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Javascript best practices for variables and functions

sashimi17sashimi17 DallasPosts: 7Member

I've used several different languages and I was wondering what the "best practices" are for writing javascript variable and function names so that I don't annoy everyone I end up working with by using non-standard naming conventions.

If I'm writing a variable for last name, should I format it:

var last_name;
var lastName;

or some other way?

If I'm writing a function, how would I format it?

LastName()

Last_Name()

or is there a better way?

Are there any other naming/commenting standards I should know about?

Answers

  • IcepickleIcepickle GermanyPosts: 49Member

    there are some naming conventions that we use at our company, following jslint proposals

    variable names, start with lowercase, each new word starts with one uppercase

    var lastName, firstName;

    functions are a bit more "intresting", when it should be an object that would be a "class" of it's own, use:

    function Person() {
        // do something
    }
    

    which you would call with:
    var person = new Person();

    function Person(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    
        this.getFullName = function() {
                return this.firstName + ' ' + this.lastName;
        }
    }
    

    some info on the jslint: http://www.jslint.com/lint.html

    it's always good if the editor you are working in is also helping you, Brackets is actually doing a pretty good job at it :)

Sign In or Register to comment.