继承

2016-12-15  本文已影响0人  智多牛

Father.js

/**
 * Father - 父对象
 */
function Father(name)
{
    /**
     * 私有属性:
     */
    var wife = "嫦娥";
    
    /**
     * 公开属性
     */
    this.name = "康熙";
    
    /**
     * 模拟一个构造函数
     */
    this._constructor = function()
    {
        if(name) this.name = name;
        console.log("Father的构造函数,父亲的名字是:"+name);
    }
    
    /**
     * 私有方法
     */
    var setMoney = function(num)
    {
        return num;
    }
    
    /**
     * 公开方法
     */
    this.eat = function(name)
    {
        return "喜欢吃:"+name;
    }
    
    this._constructor();
}

/**
 * 静态属性
 */
Father.car = "奥迪";

/**
 * 静态方法
 */
Father.driveCar = function()
{
    return Father.car;
}

Son.js

/**
 * Son - 子对象
 */
function Son()
{
    /**
     * 实现继承
     */
    Father.call(this,'成龙');
}

new Son().eat('面包')
上一篇 下一篇

猜你喜欢

热点阅读