函数式继承示例
<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>
TAG: