⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dynamicprototypepolygonexample.htm

📁 java教程!作为学习的必须过程
💻 HTM
字号:
<html>
<head>
<title>Example</title>
</head>
<body>
<script type="text/javascript">

function Polygon(iSides) {
    this.sides  = iSides;
    
    if (typeof Polygon._initialized == "undefined") {

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



function Triangle(iBase, iHeight) {
    Polygon.call(this, 3);
    this.base = iBase;
    this.height = iHeight;
    
    if (typeof Triangle._initialized == "undefined") {
        
        Triangle.prototype.getArea = function () {
            return 0.5 * this.base * this.height;
        };    
        
        Triangle._initialized = true;
    }
}

Triangle.prototype = new Polygon();

function Rectangle(iLength, iWidth) {
    Polygon.call(this, 4);
    this.length = iLength;
    this.width = iWidth;
    
    if (typeof Rectangle._initialized == "undefined") {
    
        Rectangle.prototype.getArea = function () {
            return this.length * this.width;
        };    
        
        Rectangle._initialized = true;
    }
}

Rectangle.prototype = new Polygon();


var triangle = new Triangle(12, 4);
var rectangle = new Rectangle(22, 10);

alert(triangle.sides);
alert(triangle.getArea());

alert(rectangle.sides);
alert(rectangle.getArea());

</script>
 
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -