nodejs实现继承的简单代码示例

 

  nodejs代码

 

var util = require('util');
function Base() {
    this.name = 'base';
    this.base = 1991;
    this.sayHello = function () {
        console.log('Hello ' + this.name);
    };
}
//这里声明了一个原型函数,只有原型函数才能被继承
Base.prototype.showName = function () {
    console.log(this.name);
};
function Sub() {
    this.name = 'sub';
}
util.inherits(Sub ,Base);
var objBase = new Base();
objBase.showName();
objBase.sayHello();
console.log(objBase);
var objSub = new Sub();
objSub.showName();
// objSub.sayHello();
console.log(objSub);
/*
 base
 Hello base
 { name: 'base', base: 1991, sayHello: [Function] }
 sub
 { name: 'sub' }
 */

  

posted @ 2016-04-14 13:42  南非波波  阅读(316)  评论(0编辑  收藏  举报