OOP Javascript - ES5
function Person(_name, _age, _height) {
var name = _name;
var age = _age;
var height = _height;
this.initialize = function () {
console.log('Init function');
};
function privateFunc() {
console.log('Private function');
}
this.teste = function () {
privateFunc();
};
this.sayHello = function (name) {
console.log('Hello ' + name);
};
this.hello = function hello(name) {
console.log('Hello ' + name);
};
this.getName = function getName() {
return name;
};
this.setName = function setName(_name) {
name = _name;
};
this.getAge = function () {
return age;
};
this.setAge = function (_age) {
age = _age;
};
this.getHeight = function getHeight() {
return height;
};
this.setHeight = function setHeight(_height) {
height = _height;
};
}
Person.static_method = function () {
console.log('This is my first static method');
};
Person.static_attr = 'My first static attribute';
Person.prototype.myProtoFunc = function () {
console.log('First prototype function');
};
Person.prototype.happyBirthday = function () {
return this.setAge(this.getAge() + 1);
};
function Employee(_name, _age, _height) {
var salary;
Person.call(this, _name, _age, _height);
this.getSalary = function () {
return salary;
};
this.setSalary = function (_salary) {
salary = _salary;
};
}
Employee.prototype = Object.create(Person.prototype);
var jean = new Person('Jean', 48, 1.76);
var jean2 = new Employee('Jean', 48, 1.76);
console.log(jean2.getAge());
console.log(jean2);
jean2.happyBirthday();
console.log(jean2.getAge());