1. 程式人生 > >JavaScript 繼承總結

JavaScript 繼承總結

 http://blog.csdn.net/yincheng01/article/details/6841953 Metro C++

http://www.cnblogs.com/michaelxu/archive/2008/09/20/1293716.html 執行緒同步

JavaScript 繼承總結。三角形和四邊形都是繼承自多邊形

        //多邊形
        function Polygon(iSides) {
            this.sides = iSides;
        }

        Polygon.prototype.getArea = function () {
            return 0;
        };

        //三角形
        function Triangle(iBase, iHeight) {
            Polygon.call(this, 13);
            this.base = iBase;
            this.height = iHeight;
        }
        Triangle.prototype = new Polygon();
        Triangle.prototype.getArea = function () {
            return 0.5 * this.base * this.height;
        };

        //四邊形
        function Rectangle(iLength, iWidth) {
            Polygon.call(this, 14);
            this.length = iLength;
            this.width = iWidth;
        }

        Rectangle.prototype = new Polygon();
        Rectangle.prototype.getArea = function () {
            return this.length * this.width;
        };
    
     //輸出結果
        var triangle = new Triangle(122, 4);
        var rectangle = new Rectangle(222, 10);
        alert(triangle.sides); //結果:13
        alert(triangle.getArea()); //結果:244

        alert(rectangle.sides); //結果:14
        alert(rectangle.getArea()); //結果:2220