码迷,mamicode.com
首页 > 编程语言 > 详细

javascript 继承

时间:2017-06-06 14:19:33      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:log   this   name   div   function   alert   nes   call   rip   

    //对象冒充实现继承
    function Person() {
        this.speak = function () {
            alert("我是人类");
        };
    }

    function Chinese() {
        Person.call(this);
    }
    var p = new Chinese();
    p.speak();

//对象冒充与原型继承
    function Person(sColor) {
        this.color = sColor;
    }

    Person.prototype.sayColor = function () {
        alert(this.color);
    };

    function Chinese(sColor, sName) {

        Person.call(this, sColor);//对象冒充继承属性

        this.name = sName;
    }

    Chinese.prototype = new Person();//原型继承方法

    Chinese.prototype.sayName = function () {
        alert(this.name);
    };

    var p = new Chinese("red", "高聪");
    p.sayColor();
    p.sayName();

 

javascript 继承

标签:log   this   name   div   function   alert   nes   call   rip   

原文地址:http://www.cnblogs.com/gaocong/p/6951064.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!