首页 > Web开发 > 详细

javascript 继承

时间:2014-07-22 23:05:03      阅读:433      评论:0      收藏:0      [点我收藏+]

        function Shape(id) {
            this.id = id;
        }
        Shape.prototype.name = "shape";
        Shape.prototype.toString = function () {
            var result = [];
            if (this.constructor.parent) {
                result[result.length] = this.constructor.parent.toString();
            }
            result[result.length] = this.name;
            return result.join(‘, ‘);
        };

        function TwoDShape(id) {
            TwoDShape.parent.constructor.call(this, id);
        }
        //TwoDShape.prototype = new Shape();
        //TwoDShape.prototype = Shape.prototype;
        var F = function () {
        }
        F.prototype = Shape.prototype;
        TwoDShape.prototype = new F();
        TwoDShape.prototype.constructor = TwoDShape;
        TwoDShape.parent = Shape.prototype;
        TwoDShape.prototype.name = "2D shape";

        function Triangle(id, side, height) {
            Triangle.parent.constructor.call(this, id);
            this.side = side;
            this.height = height;
        }

        //Triangle.prototype = new TwoDShape();
        //Triangle.prototype = TwoDShape.prototype;
        var F = function () {
        }
        F.prototype = TwoDShape.prototype;
        Triangle.prototype = new F();
        Triangle.prototype.constructor = Triangle;
        Triangle.parent = TwoDShape.prototype;
        Triangle.prototype.name = "triangle";
        Triangle.prototype.getArea = function () {
            return this.side * this.height / 2;
        }

        // YUI
        function extend(Child, Parent) {
            var F = function () {
            };
            F.prototype = Parent.prototype;
            Child.prototype = new F();
            Child.prototype.constructor = Child;
            Child.parent = Parent.prototype;
        }

        function Person(name){
            this.name = name;
        }
        Person.prototype.say = function(){
            return "Hi, I‘ m " + this.name + ". ";
        }
        
        extend(Developer,Person);
        function Developer(name, language){
            this.language = language;
            Developer.parent.constructor.call(this,name);
        }
        Developer.prototype.say = function(){
            return Developer.parent.say.call(this) + "I love " + this.language + ".";
        }

        /*
        var javaDeveloper = new Developer("A","Java");
        var javascriptDeveloper = new Developer("B","Javascript");
        alert(javaDeveloper.say());
        alert(javascriptDeveloper.say());
        */

javascript 继承

原文:http://www.cnblogs.com/huang_yong_jian/p/3516752.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!