함수식 상속 예

9611 단어 계승
<html>

<head>

<title></title>

<script language="javascript" type="text/javascript">

function print(msg){

  document.write(msg);      

}



function println(msg){

    print(msg + "<br/>");

}

// 

function Shape(edges){

    var _edges = edges;

    

    var that = {

        getArea : function(){

            return -1;

        },

        getEdges : function(){

            return _edges;

        }

    };

    

    return that;

}

// 

function Triangle(bottom, height)

{

    var that = Shape(3);

    

    var _bottom = bottom;

    var _height = height;

    // 

    that.getArea = function(){

        return 0.5 * _bottom * _height;

    };

    

    return that;

}

// 

function Rectangle(bottom, height)

{

    var that = Shape(4);

    

    var _bottom = bottom;

    var _height = height;

    // 

    that.getArea = function(){

        return _bottom * _height;

    };

    

    return that;

}

</script>

</head>



<body>

<script language="javascript" type="text/javascript">

var triangle = Triangle(4, 5);

println(triangle.getEdges());

println(triangle.getArea());



println("---------------------");



var rectangle = Rectangle(4, 5);

println(rectangle.getEdges());

println(rectangle.getArea());

</script>

</body>

</html>

좋은 웹페이지 즐겨찾기