nikolayneykov

Untitled

Nov 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 8.16 KB | None | 0 0
  1. # JavaScript Track Technical Questionnaire
  2.  
  3. ## Most Important Questions
  4.  
  5. ---
  6.  
  7. ### JavaScript Core
  8.  
  9. - Какво е **JavaScript**?
  10.   - **JavaScript** е слабо типизиран, прототипно базиран,
  11.   мултипарадигмов програмен език с функции от първи клас.
  12.   - Подходящ е за писане на **web**, **mobile**, и **desktop** апликации.
  13.   - На повърхността си изглежда като интерпретативен език за програмиране,
  14.   но в действителност e **just-in-time compiled** език за програмиране
  15.   - Стандарта за **JavaScript** се нарича **ECMAScript**.
  16.  
  17. - Какво е **nodejs**?
  18.   - **node.js** е **open source** платформа за **development**, която ни позволява да
  19.     изпълняваме **JavaScript** код **server-side**
  20.   - базиран е на **Google's V8 engine**
  21.  
  22. - Каква е разликата между  **var**, **let** и **const**?
  23.   - С ключовите думи **var**, **let** и **const** декларираме (създаваме) променливи.
  24.     - **var**: няма **block scoping**, има **function scoping**, стоността на променливата може да бъде **re-assigned**
  25.     - **let**: има **block scoping**, има **function scoping**, стоността на променливата може да бъде **re-assigned**
  26.     - **const**: има **block scoping**, има **function scoping**, стоността на променливата **НЕ** може да бъде **re-assigned**
  27.  
  28.       ``` js
  29.       const adult = true;
  30.  
  31.       if (adult) {
  32.           var name = "Niki";
  33.  
  34.           let age = 27;
  35.           console.log(age); // 27
  36.  
  37.           age = 28;
  38.           console.log(age); // 28
  39.  
  40.           adult = false; // Error!
  41.       }
  42.  
  43.       console.log(name); // Niki
  44.       console.log(age); // Error!
  45.       ```
  46.  
  47. - Каква е разликата между **function declaration**, **function expression** и **arrow function**?
  48.   - **function declaration** трябва да има име
  49.  
  50.     ```js
  51.     function helloWorld () {
  52.       console.log('Hello World!');
  53.     }
  54.     ```
  55.  
  56.   - **function expression** трябва да се присвои към променлива
  57.  
  58.     ```js
  59.     const helloWorld = function () {
  60.       console.log('Hello World!');
  61.     }
  62.     ```
  63.  
  64.   - **arrow function** няма **arguments object** и няма собствен **this context**
  65.  
  66.     ```js
  67.     const helloWorld = () => {
  68.       console.log('Hello World!');
  69.     }
  70.     ```
  71.  
  72. - Какво е **scope**? Какво е **closure**?
  73.   - **scope** е набор от правила за това как можем да достъпваме променливите с тяхното име
  74.  
  75.   ```js
  76.   function foo(a) {
  77.     console.log(a); // 2
  78.   }
  79.  
  80.   foo(2);
  81.   ```
  82.  
  83.   > **How it is done?**
  84.   **1.** Engine ask for foo
  85.   **2.** Scope finds it in the global scope
  86.   **3.** Engine executes foo()
  87.   **4.** Engine asks for a
  88.   **5.** Scope finds it in the function scope
  89.   **6.** The same happens with console and its method log. Scope finds them and pass them to the engine to use
  90.  
  91.   - **nested scope** (често имаме повече от един **scope**)
  92.  
  93.   ```js
  94.   function foo(a) {
  95.     console.log(a + b);
  96.   }
  97.  
  98.   var b = 2;
  99.   foo( 2 );
  100.   ```
  101.  
  102.   > **The steps of the previous slide are the same**
  103.     **1.** Engine asks the Scope of foo() (functional scope) for the variable b. But there is no b here.
  104.     **2.** Ok. What about the outside Scope (global scope in this case). Yes there is variable b here.
  105.  
  106.   - **scope rules:**
  107.     - **Engine** starts at the currently **executing scope** (looks for the variable there)
  108.     - If **the variable** is **not found** keeps going one level up and so on…
  109.     - If the outermost **global scope** is reached the search stops, whether it finds the variable or not.
  110.  
  111.   - **errors**
  112.     - Reference error
  113.       - When the variable is failed to be found nowhere in the Scopes
  114.     - Type error
  115.       - When the search for the variable is successful but you try to do an illegal action with it
  116.  
  117.   - **lexical scope**
  118.     - where variables and blocks of scope are authored
  119.     - by you, at write time
  120.  
  121.     ```js
  122.     function foo (a) {
  123.         var b = a * 2;
  124.  
  125.         function bar (c) {
  126.           console.log(c, b, a);
  127.         }  
  128.  
  129.         bar(b * 3);
  130.     }
  131.  
  132.     foo(2);
  133.     ```
  134.  
  135.     > **How it is done?**
  136.     **1.** Engine executes console.log
  137.     **2.** It needs three variables a, b and c
  138.     **3.** It searches the current scope for c
  139.     **4.** It finds c in the function scope of bar()
  140.     **5.** It searches the current scope for b but there is no such a variable
  141.     **6.** It goes one level up and searches b in the function scope of foo()
  142.     **7.** It finds b in the function scope of foo()
  143.     **8.** The same is steps for b are repeated for a
  144.  
  145. - Какво е **Module**? Защо използваме **Modules**?
  146.  
  147.   - Модул е малка единица код която можем да преизползваме.
  148.   - Модулите са лесни за използване и създават енкапсулация за нашия код.
  149.   - Модулите ни дават по-добра организация на кода и с тяхна помощ избягваме замърсяването на глобалния скоуп.
  150.  
  151. - Какво е **this context**?
  152.   - ключовата дума **this** реферира към обекта от където дадена функция е била извикана.
  153.     > в долния пример **this** ще реферира към обекта person и съответно **this.name** ще бъде 'Maria'
  154.  
  155.     ```js
  156.     const person = {
  157.       name: 'Maria',
  158.       age: 25,
  159.       sayHello: function() {
  160.         console.log(`Hello, I'm ${this.name}`);
  161.       },
  162.     };
  163.  
  164.     person.sayHello();
  165.     ```
  166.  
  167. - **Map**, **Filter** и **Reduce**.
  168.   - **map**, **filter** и **reduce** са **Higher Order Functions** (приемат функция като параметър)
  169.   - те не мутират оригиналния масив, вместо това връшат нова масив.
  170.   - **map()**
  171.     - създава нов масив използвайки стойностите на друг масив (без да променя оригиналния масив)
  172.  
  173.       ```js
  174.       const people = [
  175.         {name: 'John', age: 20},
  176.         {name: 'Mike', age: 16},
  177.         {name: 'Jenny', age: 22},
  178.       ];
  179.  
  180.       const peopleAgePlus10 = people.map((person) => {
  181.         person.age += 10;
  182.         return person;
  183.       });
  184.  
  185.       // [ { name: 'John', age: 30 },
  186.       // { name: 'Mike', age: 26 },
  187.       // { name: 'Jenny', age: 32 } ]
  188.       ```
  189.  
  190.   - **filter()**
  191.     - Създава нов масив базиран на това дали елементите на даден масив отговарят на дадено условие (предикат)
  192.  
  193.       ```js
  194.       const people = [
  195.         {name: 'John', age: 20},
  196.         {name: 'Mike', age: 16},
  197.         {name: 'Jenny', age: 22},
  198.       ];
  199.  
  200.       const peopleLegalAge = people.filter((person) =>
  201.         person.age >= 18
  202.       );
  203.  
  204.       // [ { name: 'John', age: 20 },
  205.       // { name: 'Jenny', age: 22 } ]
  206.       ```
  207.  
  208.   - **reduce()**
  209.     - използва акумолатор за да редуцира всички стойности от даден масив до една стойност.
  210.     - резултата от редуцирането може да бъде от всякакъв тип (**array**, **object**, **boolean**, etc.)
  211.  
  212.         ```js
  213.         const people = [
  214.           {name: 'John', age: 20},
  215.           {name: 'Mike', age: 16},
  216.           {name: 'Jenny', age: 22},
  217.         ];
  218.  
  219.         const totalAge = people.reduce((accumolator, person) => {
  220.           accumolator += person.age;
  221.           return accumolator;
  222.         }, 0);
  223.  
  224.         // totalAge 58
  225.         ```
Add Comment
Please, Sign In to add comment