Страница 1 из 1

Упрощение кода

Добавлено: 27 янв 2013, 17:58
axioma

Код: Выделить всё

<HTML>
 <HEAD>
 <TITLE> Вычисление периметра и площади</TITLE>
 <script language="JavaScript">
     function Figure(name, length, perimetr, square) 
     {
         this.name = name;
         this.length = length;
         this.perimetr = perimetr;
         this.square = square;
     }
     Figure.prototype.get_perimetr = function () {
         return this.perimetr;
     };
     Figure.prototype.get_square = function () {
         return this.square;
     };

     Foursquare.prototype = new Figure();
     Foursquare.prototype.constructor = Foursquare;

     function Foursquare(length) {
         Figure.call(this, "Square", length, length*4, length*length);
     };
     
     function Foursquare_do() {
         var new_Foursquare = new Foursquare(document.getElementsByName("st1")[0].value);
         document.getElementsByTagName("div")[0].innerHTML = 'Площадь квадрата: ' + new_Foursquare.get_square() + '
Периметр квадрата: ' + new_Foursquare.get_perimetr();

     };

	 Triangle.prototype = new Figure();
	 Triangle.prototype.constructor = Triangle;

	 function Triangle(length) {
	     Figure.call(this, "Triangle", length, length * 3, length * length * 0.25 * Math.sqrt(3));
	 };

	 function Triangle_do() {
	     var new_Triangle = new Triangle(document.getElementsByName("st1")[0].value);
	     document.getElementsByTagName("div")[1].innerHTML = 'Площадь треугольника: ' + new_Triangle.get_square() + '
Периметр треугольника: ' + new_Triangle.get_perimetr();
	 };

	 Circle.prototype = new Figure();
	 Circle.prototype.constructor = Circle;

	 function Circle(length) {
	     Figure.call(this, "Circle", length * 2 * 3.14, length * length * 3.14);
	 };
	function Circle_do() {
        var new_Circle = new Circle(document.getElementsByName("st1")[0].value);
	    document.getElementsByTagName("div")[2].innerHTML = 'Площадь круга: ' + new_Circle.get_square() + '
Периметр круга: ' + new_Circle.get_perimetr();
	};

	function calculate() {
	    Foursquare_do();
	    Triangle_do();
	    Circle_do();
	}
</script>
</HEAD>
<BODY>
[align=center] 
<table border="1" cellpadding="130">
<tr>
<td>
[align=center]
Вычисление площади и периметра геометрических фигур
<h4>Введите значениe стороны:</h4>
 <form name="form2">
     Сторона: <input type="text" size="7" name="st1" 
         style="width: 52px">

     <input type="button" value="Вычислить" onClick="calculate()">

 <div>
 </div>
 <div>
 </div>
 <div>
 </div>
[/align] 
</td>
</tr>
</table>
[/align]
 </form>
 </BODY>
 </HTML>
Помогите мне сделать, чтобы функции function Foursquare_do(), function Triangle_do() и function Circle_do(), которые выводят всё на экран сделать в классе Figure, так чтобы эти почти одинаковые функции объединить в одну.

Re: Упрощение кода

Добавлено: 31 янв 2013, 10:21
Хыиуду

Код: Выделить всё

    function Figure_do(fig) {
        var size=document.getElementsByName("st1")[0].value;
        if (fig=='circle) {
            fig = new Circle(size);
			var name='круга';
		}
		else if (fig=='triangle')	{
			fig = new Triangle(size);
			var name='треугольника';
		}
		else if (fig=='foursquare')	{
			fig = new Foursquare(size);
			var name='квадрата';
		}
          document.getElementsByTagName("div")[0].innerHTML += 'Площадь '+name+': ' + fig.get_square() + '
Периметр '+name+': ' + fig.get_perimetr();
    };