Node.js:util.inherits 面向對象特性【原型】

/**
 * Created by Administrator on 2014/9/4.
 */
var util = require('util');
function Base() {
    this.name = '李斌';
    this.base = '1992';
    this.sayHello = function() {
        console.error('Hello' + this.name);
    }
}

Base.prototype.showName = function() {
    console.log(this.name);
}

function Sub() {
    this.name = '小樣';
    this.base = '1993';
}

util.inherits( Sub , Base );

var objBase = new Base();
var objSub  = new Sub();

objBase.showName();
objBase.sayHello();
console.error(objBase);

objSub.showName();/* showName爲Baes的原型函數,容許被子類繼承並調用 */
//objSub.sayHello();/* sayHello爲Base內部構造的函數不容許被繼承調用 */
console.log(objSub);
相關文章
相關標籤/搜索