iife (immediately invoked function expression)

PPTX 9 pages 231.1 KB Free download

Page preview (5 pages)

Scroll down 👇
1 / 9
tutorial 3: iife, context, new 3.1 iife (immediately invoked function expression) an iife is a function that runs as soon as it is defined. it helps avoid polluting the global scope by encapsulating code. (function () { let secret = "this is hidden"; console.log(secret); // output: this is hidden })(); // console.log(secret); // error: secret is not defined real-world example : initializing a library or module without exposing internal variables. (function () { let apikey = "12345"; function getapikey() { return apikey; } console.log(getapikey()); // output: 12345 })(); 3.2 context (this) the this keyword refers to the context in which a function is called. its value depends on how the function is invoked. 3.2.1 implicit binding when a function is called as a method of an object, this refers to the object. const person = { name: "alice", greet: function () { console.log(`hello, my name is ${this.name}`); }, }; person.greet(); …
2 / 9
gdetails.call(user); // output: user: user, role: regular user 3.3 new keyword the new keyword creates an instance of an object from a constructor function. it sets up the prototype chain and initializes the object. function car(make, model) { this.make = make; this.model = model; } const mycar = new car("toyota", "corolla"); console.log(mycar.make); // output: toyota real-world example : creating multiple instances of a product in an e-commerce system. function product(name, price) { this.name = name; this.price = price; } const laptop = new product("laptop", 1000); const phone = new product("phone", 500); console.log(laptop.name); // output: laptop console.log(phone.price); // output: 500
3 / 9
iife (immediately invoked function expression) - Page 3
4 / 9
iife (immediately invoked function expression) - Page 4
5 / 9
iife (immediately invoked function expression) - Page 5

Want to read more?

Download all 9 pages for free via Telegram.

Download full file

About "iife (immediately invoked function expression)"

tutorial 3: iife, context, new 3.1 iife (immediately invoked function expression) an iife is a function that runs as soon as it is defined. it helps avoid polluting the global scope by encapsulating code. (function () { let secret = "this is hidden"; console.log(secret); // output: this is hidden })(); // console.log(secret); // error: secret is not defined real-world example : initializing a library or module without exposing internal variables. (function () { let apikey = "12345"; function getapikey() { return apikey; } console.log(getapikey()); // output: 12345 })(); 3.2 context (this) the this keyword refers to the context in which a function is called. its value depends on how the function is invoked. 3.2.1 implicit binding when a function is …

This file contains 9 pages in PPTX format (231.1 KB). To download "iife (immediately invoked function expression)", click the Telegram button on the left.

Tags: iife (immediately invoked funct… PPTX 9 pages Free download Telegram