javascript hoisting tutorial

PPTX 10 стр. 234,0 КБ Бесплатная загрузка

Предварительный просмотр (5 стр.)

Прокрутите вниз 👇
1 / 10
tutorial 2: hoisting, let vs const, closures 2.1 hoisting hoisting is javascript's behavior of moving declarations to the top of their scope during compilation. this means that you can use variables and functions before they are declared in your code. 2.1.1 variable hoisting only declarations are hoisted, not initializations. variables declared with var are initialized with undefined. console.log(x); // output: undefined var x = 10; // equivalent to: var x; console.log(x); // output: undefined x = 10; real-world example : imagine you declare a variable at the bottom of your script, but you try to access it at the top. without understanding hoisting, this could lead to unexpected behavior. function checkuser() { console.log(user); // output: undefined var user = "alice"; console.log(user); // output: alice } checkuser(); 2.1.2 function hoisting functions declared using the function keyword are fully hoisted, meaning you can call them before they are defined. greet(); // output: …
2 / 10
properties or elements can still be modified. const pi = 3.14; // pi = 3.14159; // error: assignment to constant variable const user = { name: "alice" }; user.name = "bob"; // valid console.log(user.name); // output: bob real-world example : a configuration object that should not be reassigned, but its properties can be updated. const config = { apiurl: "https://api.example.com", timeout: 5000 }; config.timeout = 10000; // valid console.log(config.timeout); // output: 10000 2.3 closures a closure is a function that retains access to its lexical scope, even when executed outside that scope. closures are powerful for creating private variables and encapsulating functionality. function outer() { let message = "hello"; function inner() { console.log(message); // accesses 'message' from outer scope } return inner; } const closurefunc = outer(); closurefunc(); // output: hello real-world example : a function that generates unique ids. function createidgenerator() { let id = 0; return function () …
3 / 10
javascript hoisting tutorial - Page 3
4 / 10
javascript hoisting tutorial - Page 4
5 / 10
javascript hoisting tutorial - Page 5

Хотите читать дальше?

Скачайте все 10 страниц бесплатно через Telegram.

Скачать полный файл

О "javascript hoisting tutorial"

tutorial 2: hoisting, let vs const, closures 2.1 hoisting hoisting is javascript's behavior of moving declarations to the top of their scope during compilation. this means that you can use variables and functions before they are declared in your code. 2.1.1 variable hoisting only declarations are hoisted, not initializations. variables declared with var are initialized with undefined. console.log(x); // output: undefined var x = 10; // equivalent to: var x; console.log(x); // output: undefined x = 10; real-world example : imagine you declare a variable at the bottom of your script, but you try to access it at the top. without understanding hoisting, this could lead to unexpected behavior. function checkuser() { console.log(user); // output: undefined var user = "alice"; …

Этот файл содержит 10 стр. в формате PPTX (234,0 КБ). Чтобы скачать "javascript hoisting tutorial", нажмите кнопку Telegram слева.

Теги: javascript hoisting tutorial PPTX 10 стр. Бесплатная загрузка Telegram