Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Технологии разработки клиентских WEB-приложений на языке JavaScript. Учебное пособие

.pdf
Скачиваний:
0
Добавлен:
07.09.2026
Размер:
1 Мб
Скачать
21
        
if (x == "value1") action1(); else if (x == "value2") action2(); else if (x == "value3") action3(); else defaultAction();
switch (prompt("What is the weather like?")) { case "rainy":
console.log("Remember to bring an umbrella."); break;
case "sunny":
console.log("Dress lightly.");
case "cloudy":
console.log("Go outside."); break;
default:
console.log("Unknown weather type!"); break;
}
     
                                               
  continue   break                         
Оператор выбора SWITH
   
   switch,               JavaScript            C / Java),      if     
                           
         
22
     break.    
𝑥 𝑙𝑛 󰈏󰇛𝑦
𝑧
󰇜󰇛𝑧
𝑦
𝑎
𝑧
󰇜󰈏
                                                
Практическое задание
 1
    x  z.
       x.
    x  z.
     z,   alert.
 2
            
 3
     
Работа с функциями и классами
Обзор функций JavaScript
       JavaScript,         
  JavaScript      function,         ().                            ...).           {}.
         
23
       
function myFunction(p1, p2) {
return p1 * p2;
}
myFunction(3,2) //     6
   const ordinary2 = function (p1, p2) { return p1 * p2; }; ordinary2(4,2) //     8
   const ordinary3 = function myName(p1, p2) { return p1 * p2; }; ordinary3(4,3) //     12
    2  p1 p2, 
const obj = { addAsMethod: myFunction }; obj.addAsMethod(2, 4) //     8
function funcExpr(num) { if (num < 15) {
console.log(num);     return funcExpr(++num);
} return num; } funcExpr(12);
      
  return           undefined.       return,   undefined.
         
            
          
24
    funcExpr   
const func = function funcExpr() { return funcExpr }; func() //   funcExpr() funcExpr() //        Refer-
enceError
const f = function (x, y, z) { return 123 };
const f = (x, y, z) => { return 123 };
const f = (x, y, z) => 123;
const id = x => x;
> [1,2,3].map(x => x+1) [ 2, 3, 4 ]
[1,2,3].map(function (x) { return x+1 });
 4    12  15.
                
 JavaScript             
  
    
       
                
             
             
                  
25
  JavaScript      
function hypotenuse(a, b) { function square(x) { return x*x; } return Math.sqrt(square(a) + square(b)); }
class User { //   }
const UserClass = class{ //   }
class User { name; constructor(name){ this.name = name; //     
 
}} const user = new User('Hello') //     user.name; //   name),   
'Hello'
             
   square       b     
Обзор классов JavaScript
         class         
  
 
            
     constructor  1,  2,        
 
         static.
  JavaScript,        
26
        
class User { static #takenNames = []; static isNameTaken(name) { return User.#takenNames.includes(name); } name = 'Unknown'; constructor(name) { this.name = name; User.#takenNames.push(name); } } const user = new User('Jon Snow'); User.isNameTaken('Jon Snow'); // => true User.isNameTaken('Arya Stark'); // => false
class User { #name; //   constructor(name) { this.#name = name; } getName() { return this.#name; } } const user = new User('Hello');
   
isNameTaken()      
  User.#takeNames     name.
                   
 JavaScript     
                 #.
       
 
 getName:
      
        
27
user.#name; // SyntaxError is thrown user.getName();
      ex-
class ContentWriter extends User { posts = []; }
tends.
   User    
 ContentWriter       User.
               super().            super.getName().
  JavaScript            JavaScript                       
Практическое задание
 
                                      12321.
      
1.          
      2             
2.         
                              5.
           1   2.
 
   Car (       
28
1.         
            );
2.         
           
3.        
       
                        3)   
 
   TV (      
1.          
  
2.         
              
3.       
             100     0     300   0;
4.        
           
                                             4)   
Стандартные встроенные объекты JavaScript
Числа и даты
         
Number;
Math;
Date.
29
Math        
function randomPointOnCircle(radius) { let angle = Math.random() * 2 * Math.PI; return {x: radius * Math.cos(angle),
y: radius * Math.sin(angle)}; } console.log(randomPointOnCircle(2)); // {x: 0.3667, y: 1.966}
Math.max  Math.min  Math.sqrt  
 Math              Math,                           
                      -          - max          JavaScript  max      Math,       
                  JavaScript          let  const,              var  func-
tion.
   Math.            cos (), sin (  tan      , acos, asin  atan    (pi) ­           JavaScript -   Math.PI.            
        -                                                               
            Math.floor         Math.random.
30
console.log(Math.floor(Math.random() * 10)); // 2
    10       0
Number(true); // returns 1 Number(false); // returns 0
Number("10"); // returns 10 Number(" 10"); // returns 10 Number("10 "); // returns 10 Number(" 10 "); // returns 10 Number("10.33"); // returns 10.33 Number("10,33"); // returns NaN Number("10 33"); // returns NaN Number("John"); // returns NaN
Number(new Date("2017-09-30")); // returns 1506729600000
new Date();
new Date(value);
new Date(dateString);
new Date(year, month[, day[, hour[, minute[, second[, millisecond]]]]]);
  10.  Math.floor               0  9.
   Math.ceil         Math.round      Math.abs,                 
   3.14  2014)            
  JavaScript            JavaScript          
Number ()      JavaScript 
Number ()     
  Number ()   
1.1.1970.
  Date,    
Date         
 Date       
Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]