Created
March 22, 2012 13:35
-
-
Save h4/2158367 to your computer and use it in GitHub Desktop.
JS.5.функции
This file contains hidden or 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
/* | |
Функции | |
*/ | |
// 1. Определение и вызов функции | |
function yourMessage(){ | |
console.log("функция"); | |
} | |
yourMessage(); | |
// 2. Параметры, передаваемые функции | |
function myfunction(txt){ | |
console.log(txt); | |
} | |
myfunction('Hello'); | |
// 3. Оператор return. Возвращаемое значение функции | |
function product(a,b) | |
{ | |
c = a*b; | |
return c; | |
} | |
console.log(product(4,3)); | |
// 4. Область видимости переменной | |
var g="global"; | |
function check() | |
{ | |
var g = "local"; | |
document.write(g) | |
} | |
check(); | |
console.log(g); | |
// Этот пример надо сделать дважды: | |
// 1 - выделенное жирным шрифтом слово присутствует в тексте, | |
// 2 - выделенное жирным шрифтом слово отсутствует. | |
// Отметьте разницу. | |
// 5. Рекурсивные функции | |
function factorial(n) { | |
if (n <= 1) | |
return 1; | |
else | |
return (n * factorial(n-1)); | |
} | |
// 6. Функции как данные | |
function square(x){ | |
return x*x; | |
} | |
var a = square(5); | |
console.log(a); | |
var b = square; | |
var c = b(3); | |
console.log(c); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment