代码改变世界

JavaScript 中的对象与继承

2013-05-10 10:59  Evan.Pei  阅读(155)  评论(0)    收藏  举报
    //动物类
    function Animal() {
        this.species = 'animal';
        this.sleep = function() { alert(this.species); }
    }
    //猫类
    function cat1(name, color) {
        Animal.apply(this); //apply(),call();实现cat1继承Animal
        this.name = name;
        this.color = color;
        this.show = function() {
            document.writeln('我的名字是:' + this.name + ',是只:' + this.color + '颜色的猫!');
        };
    }
    var c1 = new cat1('lihaoo', 'black');
    c1.sleep();//调用父类方法
c1.show();//调用子类方法