Helper method to extend class - Node.js Object

Node.js examples for Object:Inheritance

Description

Helper method to extend class

Demo Code


var object_create = Object.create || function (prototype) {
        function Type() { }
        Type.prototype = prototype;
        return new Type();
    };/*w  ww.ja  v a2 s. com*/
function extend(SubClass, SuperClass, overrides) {
    var subProto, name;
    SuperClass = SuperClass || Object;
    SubClass.prototype = object_create(SuperClass.prototype);
    subProto = SubClass.prototype;
    subProto.constructor = SubClass;
    if (overrides) {
        for (name in overrides) {
            if (overrides.hasOwnProperty(name)) {
                subProto[name] = overrides[name];
            }
        }
    }
}

function print() {
    console.log(this.name);
}
function setPersonAddress(newAddress) {
    this.address=newAddress;
}
function printAge() {
    console.log(this.age);
}
function printLevel() {
    console.log(this.level);
}
function printLovel(){
    console.log(this.lovel);
}
//var Person=Person
function Person(){
    var age;
    var lovel;
    this.getAge = function(){
        return age;
    };
    this.setAge=function (newAge){
        age=newAge;
    };
    this.getName = function(){
        return this.name;
    };
    this.setName = function(newName){
        this.name=newName;
    };
    this.getAddress=function () {
        return this.address;
    };
    this.setLovel=function (newLovel){
        this.lovel=newLovel;
    };
    this.getLovel=function () {
        return this.lovel;
    };
    this.getPersonLevelClass=function () {
        return this.level;
    };
    this.getLevel=function () {//undefined
        return this.level;
    };
    this.getPersonLevel=function () {//ReferenceError: level is not defined
        return level;
    };
    this.printAge=printAge;
    this.setPersonAddress = setPersonAddress;
    this.print=print;
    this.printLevel=printLevel;//undefined
    this.printLovel=printLovel;

}

Person.level="A";
Person.printPersonLevelClass=function () {
    console.log(Person.level);
};
Person.printPersonLevel=function () {
    console.log(this.level);
};
Person.printLevel=function () {
    console.log(level);
};


Person.prototype.setBirdDay=function (newBirdDay) {
    this.birdDay=newBirdDay;
};
Person.prototype.getBirdDay=function () {
    return this.birdDay;
};

Person.prototype.printMainInfo=function () {
    console.log( "printMainInfo start");
    console.log( this.name);
    console.log( this.age);//undinefed
    console.log( this.lovel);
    console.log( this.workSinceYear);
    console.log( Person.level);
    console.log( "printMainInfo Over");
};

function Employee() {
    Person.apply(this, arguments);
}
extend(Employee, Person, {
    employeeId: true,
    setEmployeeId: function(newEmployeeId) {
        employeeId=newEmployeeId
    },
    getEmployeeId:function () {
        return employeeId;
    },
    setDepartId: function(newDepartId) {
        departId=newDepartId;
    },
    getDepartId:function () {
        return departId;
    },
    setAlias: function(newAlias) {
        this.alias=newAlias;
    },
    getAlias:function () {
        return this.alias;
    }
});



Employee.prototype.setWorkSinceYear=function (newWorkSinceYear) {
    this.workSinceYear=newWorkSinceYear;
};
Employee.prototype.getWorkSinceYear=function () {
    return this.workSinceYear;
};

var lisi=new Employee();
lisi.setName("lisi");
console.log(lisi.getName());//BJ
console.log(lisi.name);//BJ
lisi.print();//BJ
lisi.setDepartId(100);
console.log(lisi.getDepartId());//100
console.log(lisi.departId);//undefined

lisi.setEmployeeId("emp201102");
console.log(lisi.getEmployeeId());//emp201102
console.log(lisi.employeeId);//true
lisi.setAlias("LS");
console.log(lisi.getAlias());
console.log(lisi.alias);

var emp1=new Employee();
emp1.setName("emp1");
console.log(emp1.getName());//BJ
console.log(emp1.name);//BJ
emp1.print();//BJ
console.log(emp1.getEmployeeId());//emp201102
emp1.setEmployeeId("emp201101");

console.log(emp1.getEmployeeId());//emp201101
console.log(lisi.getEmployeeId());//emp201101
console.log(emp1.employeeId);//true
console.log(emp1.getDepartId());//100
console.log(emp1.departId);//undefined
emp1.setDepartId(200);
console.log(emp1.getDepartId());//200
console.log(emp1.departId);//undefined

console.log(lisi.getDepartId());//200
console.log(lisi.departId);//undefined

emp1.setLovel("Travel");
console.log(emp1.getLovel());
emp1.printLovel();

emp1.setBirdDay(1900);
console.log(emp1.getBirdDay());

emp1.setAge(20);
console.log(emp1.getAge());//20
console.log(emp1.age);//undefined
emp1.printAge();//undefined
emp1.setPersonAddress("TianAnMei");
console.log(emp1.getAddress());

console.log("emp1.getLevel",emp1.getLevel());//undefined
console.log("emp1",emp1.getPersonLevelClass());//A
//console.log("p1.getPersonLevel()",p1.getPersonLevel());//ReferenceError: level is not defined
console.log("p1.printLevel");
emp1.printLevel();//undefined
emp1.printMainInfo();
emp1.setWorkSinceYear(19990)
console.log(emp1.getWorkSinceYear());

console.log(emp1.getAlias());//undefined
//Person.printLevel();//ReferenceError: level is not defined
Person.printPersonLevel();//A
Person.printPersonLevelClass();//A

var p1 =new Person();
p1.setName("BJ");
console.log(p1.getName());//BJ
console.log(p1.name);//BJ
p1.print();//BJ

p1.setLovel("Travel");
console.log(p1.getLovel());
p1.printLovel();

p1.setBirdDay(1900);
console.log(p1.getBirdDay());

p1.setAge(20);
console.log(p1.getAge());//20
console.log(p1.age);//undefined
p1.printAge();//undefined
p1.setPersonAddress("TianAnMei");
console.log(p1.getAddress());

console.log("p1.getLevel",p1.getLevel());//undefined
console.log("getPersonLevelClass",p1.getPersonLevelClass());//A

console.log("p1.printLevel");
p1.printLevel();//undefined
p1.printMainInfo();

Person.printPersonLevel();//A
Person.printPersonLevelClass();//A

var p2 =new Person();
p2.setName("p2");
console.log(p2.getName());
p2.print();
//
p2.setPersonAddress("WaiTang");
console.log(p2.getAddress());
p2.setLovel("Reading");
console.log(p2.getLovel());
p2.printLovel();
p2.setAge(30);
console.log(p2.getAge());//30
p2.printMainInfo();

console.log(p1.getName());
console.log(p1.getAddress());
console.log(p1.getLovel());
p1.printLovel();
console.log(p1.getAge());

Related Tutorials