آموزش مقدماتی جاوا اسکریپت مقدماتی| DESTRUCTURING| قسمت 46

ادوکر
ادوکر
8 بار بازدید - 4 ماه پیش - 00:00:00 مقدمه 00:00:21 مقادیر دو
00:00:00 مقدمه 00:00:21 مقادیر دو متغیر را عوض کنید 00:01:11 دو عنصر را در یک آرایه عوض کنید 00:02:34 عناصر آرایه را به متغیرها اختصاص دهید 00:03:47 مقادیر را از اشیا استخراج کنید 00:06:11 تخریب در پارامترهای تابع // destructuring = extract values from arrays and objects, // then assign them to variables in a convenient way // [] = to perform array destructuring // {} = to perform object destructuring // --------- EXAMPLE 1 --------- // SWAP THE VALUE OF TWO VARIABLES let a = 1; let b = 2; [a, b] = [b, a]; console.log(a); console.log(b); // --------- EXAMPLE 2 --------- // SWAP 2 ELEMENTS IN AN ARRAY const colors = ['red', 'green', 'blue', 'black', 'white']; [colors[0], colors[4]] = [colors[4], colors[0]] console.log(colors); // --------- EXAMPLE 3 --------- // ASSIGN ARRAY ELEMENTS TO VARIABLES const [firstColor, secondColor, thirdColor, ...extraColors] = colors; console.log(firstColor); console.log(secondColor); console.log(thirdColor); console.log(extraColors); // --------- EXAMPLE 4 --------- // EXTRACT VALUES FROM OBJECTS const person1 = { firstName: 'Spongebob', lastName: 'Squarepants', age: 30, job: "Fry cook", }; const person2 = { firstName: 'Patrick', lastName: 'Star', age: 34 }; const {firstName, lastName, age, job="Unemployed"} = person2; console.log(firstName); console.log(lastName); console.log(age); console.log(job); // --------- EXAMPLE 5 --------- // DESTRUCTURING IN FUNCTION PARAMETERS function displayPerson({ firstName, lastName, age, job="Unemployed" }) { console.log(`name: ${firstName} ${lastName}`); console.log(`age: ${age}`); console.log(`job: ${job}`); } displayPerson(person1); displayPerson(person2);
4 ماه پیش در تاریخ 1403/02/16 منتشر شده است.
8 بـار بازدید شده
... بیشتر