Created
May 9, 2014 17:05
-
-
Save LESTADru/f6fd496aff520f7ebc9c to your computer and use it in GitHub Desktop.
Функция конструктор сумматор, 2 варианта решения.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head lang="en"> | |
<meta charset="UTF-8"> | |
<title>функция-конструктор Summator, которая создает объект с двумя методами: | |
sum(a,b) возвращает сумму двух значений | |
run() запрашивает два значения при помощи prompt и выводит их сумму, используя метод sum.</title> | |
</head> | |
<body> | |
<script> | |
'Use Strict' | |
// 1 Вариант с помощью свойств объукта. | |
function Summator() { | |
this.sum = function(){ | |
return this.a + this.b; | |
}; | |
this.run = function() { | |
this.a = +prompt("Enter first value", "0"); | |
this.b = +prompt("Enter second value", "0"); | |
alert( this.sum() ); | |
}; | |
} | |
new Summator().run(); | |
// 2 Вариант с помощью локальных переменных. | |
function Summator1() { | |
this.sum = function(a, b){ | |
return a + b; | |
}; | |
this.run = function() { | |
var a,b; | |
a = +prompt("Enter first value", "0"); | |
b = +prompt("Enter second value", "0"); | |
alert( this.sum(a,b) ); | |
}; | |
} | |
new Summator1().run(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment