javascript hoisting tutorial

PPTX 10 sahifa 234,0 KB Bepul yuklash

Sahifa ko'rinishi (5 sahifa)

Pastga aylantiring 👇
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

Ko'proq o'qimoqchimisiz?

Barcha 10 sahifani Telegram orqali bepul yuklab oling.

To'liq faylni yuklab olish

"javascript hoisting tutorial" haqida

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"; …

Bu fayl PPTX formatida 10 sahifadan iborat (234,0 KB). "javascript hoisting tutorial"ni yuklab olish uchun chap tomondagi Telegram tugmasini bosing.

Teglar: javascript hoisting tutorial PPTX 10 sahifa Bepul yuklash Telegram