Class with encapsulation. Methods are public, members private and public.
var Person = function(name, secondName, age, height, weight, socialInsuranceNumber) {
// private properties/fields
var _name = name || "Rob Gravelle";
var _height = height || 68; //in inches
var _weight = weight || 170;
var _socialInsuranceNumber = socialInsuranceNumber || "555 555 555";
function oPerson() {
this.secondName = "Shabash" || secondName; //public memeber too
}
// public memeber
oPerson.prototype = {
setHeight: function(height) { _height=height; },
getHeight: function() { return _height; },
setWeight: function(weight) { _weight=weight; },
getWeight: function() { return _weight; },
setName: function(name) { _name=name; },
getName: function() { return _name; },
setSocialInsuranceNumber: function(socialInsuranceNumber) {
_socialInsuranceNumber=socialInsuranceNumber;
},
calculateBmi: function() {
return Math.round(( _weight * 703) / ( _height * _height));
},
age: age
};
return new oPerson();
};
//instantiate the Person class with some arguments
var aPerson = new Person("Ted Smith", "Feller", 34, 70, 175, "123 456 789");
//this displays "I am Ted Smith and and my BMI is 25."
console.log("My name is " + aPerson.getName() + ' ' + "and my BMI is " + aPerson.calculateBmi() + ".");
console.log("My second name is " + aPerson.secondName + ' ' + "and my age is " + aPerson.age + ".");