Слайд 2
![Function declaration and invocation function print(value) { console.log(value); } function](/_ipx/f_webp&q_80&fit_contain&s_1440x1080/imagesDir/jpg/122982/slide-1.jpg)
Function declaration and invocation
function print(value) {
console.log(value);
}
function isEmail(email) {
// Do validation
return true/false;
}
Print(“hello”);
Var
isValidEmail = isEmail(“a”);
Слайд 3
![Local and outer variables var name = “poghos”;//outer function showInfo()](/_ipx/f_webp&q_80&fit_contain&s_1440x1080/imagesDir/jpg/122982/slide-2.jpg)
Local and outer variables
var name = “poghos”;//outer
function showInfo() {
var message =
“hello, ” + name;// local
console.log(message);
}
Слайд 4
![Default parameters es5 function showMessage(from, text) { text = text](/_ipx/f_webp&q_80&fit_contain&s_1440x1080/imagesDir/jpg/122982/slide-3.jpg)
Default parameters
es5
function showMessage(from, text) {
text = text || “No text given”;
console.log(from
+ “: ” + text);
}
Es6
function showMessage(from, text = “No text given”) { console.log(from + “: ” + text);
}
Слайд 5
![Naming function "get…" – return a value, "calc…" – calculate](/_ipx/f_webp&q_80&fit_contain&s_1440x1080/imagesDir/jpg/122982/slide-4.jpg)
Naming function
"get…" – return a value,
"calc…" – calculate something,
"create…" – create
something,
"check…" – check something and return a boolean, etc,
“is…" – predicate.*
* A predicate is a box that takes an argument and returns a Boolean value.
Слайд 6
![Function expression and invaction var print = function (value) { console.log(value); } Print(“hello”);](/_ipx/f_webp&q_80&fit_contain&s_1440x1080/imagesDir/jpg/122982/slide-5.jpg)
Function expression and invaction
var print = function (value) {
console.log(value);
}
Print(“hello”);
Слайд 7
![Nested functions function showPrimes(n) { function isPrime(n) { for (let](/_ipx/f_webp&q_80&fit_contain&s_1440x1080/imagesDir/jpg/122982/slide-6.jpg)
Nested functions
function showPrimes(n) {
function isPrime(n) {
for (let i = 2;
i < n; i++) {
if ( n % i == 0) return false;
}
return true;
}
for (let i = 2; i < n; i++) {
if (!isPrime(i)) {
continue;
}
alert(i); // a prime
}
}
Слайд 8
![Callback functions function ask(question, yes, no) { if (confirm(question)) {](/_ipx/f_webp&q_80&fit_contain&s_1440x1080/imagesDir/jpg/122982/slide-7.jpg)
Callback functions
function ask(question, yes, no) {
if (confirm(question)) {
yes();
}
else {
no();
}
}
function showOk() {
alert( "You agreed." );
}
function showCancel() {
alert( "You canceled the execution." );
}
ask("Do you agree?", showOk, showCancel);
Слайд 9
![Arguments object Arguments is array like object function max() {](/_ipx/f_webp&q_80&fit_contain&s_1440x1080/imagesDir/jpg/122982/slide-8.jpg)
Arguments object
Arguments is array like object
function max() {
var max =
Number.NEGATIVE_INFINITY;
for(var i = 0; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
}
var largest = max(1, 10, 100, 2, 3, 1000, 4, 5, 10000, 6);
Слайд 10
![Es5 array methods forEach() var data = [1,2,3,4,5]; var sum](/_ipx/f_webp&q_80&fit_contain&s_1440x1080/imagesDir/jpg/122982/slide-9.jpg)
Es5 array methods
forEach()
var data = [1,2,3,4,5];
var sum = 0;
data.forEach(function(value, index, array)
{ sum += value; });
map()
var arr = [1, 2, 3];
var mapedArr = arr.map(function(x) { return x*x; })
filter()
var arr = [1, 2, 3];
var evenNumbers = arr.filter(function(x,i) { return x%2==0 });
every()
var arr = [1, 2, 3];
var isLess = arr.every(function(x) { return x < 10 });
Слайд 11
![Es5 array methods some() var arr = [1, 2, 3,](/_ipx/f_webp&q_80&fit_contain&s_1440x1080/imagesDir/jpg/122982/slide-10.jpg)
Es5 array methods
some()
var arr = [1, 2, 3, 15];
var isExistsGreater
= arr.some(function(x) { return x > 10 });
reduce()
var arr = [1, 2, 3];
var sum = arr.reduce(function(x, y) { return x+y; }, 0)
reduceRight()
var arr = [1, 2, 3];
var sum = arr. reduceRight(function(x, y) { return x+y; }, 0)
Слайд 12
![Any questions?](/_ipx/f_webp&q_80&fit_contain&s_1440x1080/imagesDir/jpg/122982/slide-11.jpg)