I normally use this pattern, that I haven't seen many do.
I do this to avoid having to order my methods in any special way. If all is public, then one normally has to ensure that the methods called, are declared before the method call
var person = new Person("Mo", "Yo");
person.getFullname();
person.getFirstname();
person.getLastname();
function Person(firstname, lastname) {
var firstname, lastname;
(function constructor(){
setFirstname(firstname);
setLastname(lastname)
})();
this.getFullname = getFullname; // Makes getFullName() public
function getFullname() {
// Will allow you to order method in whatever order you want.
// If we where to have it as just this.getFullname = function () {...} and same for firstname
// as it is normally done, then this.getFirstname would have to be placed before this method.
// A common pain in the ass, that you cannot order methods as you want!
return getFirstname() + ", " + getLastname();
}
this.getFirstname = getFirstname;
function getFirstname() {
return firstname;
}
function setFirstname(name){
firstname = name;
}
this.getLastname = getLastname;
function getLastname() {
return lastname;
}
function setLastname(name) {
lastname = name;
}
}
function pri() { blah; }instead of using avardeclaration.