Destruktorius. Paskirtis - 'ištraukti' kintamuosius iš objekto [masyvo] ir juos naudoti be paprasčiau:
let thingsToDo ={
morning : 'Exercise',
afternoon: 'Work',
evening: 'Code',
night: ['Sleep', 'Dream']
};
let {morning, afternoon} = thingsToDo;
morning = 'Run';
console.log(morning, ' - ', afternoon);
Rezultatas:
Kitas pvz:
let Student = student => {
console.log(`${student.name} from ${student.university}`);
}
Student({
name: 'Vytautas',
university: 'KTU'
});
Rezultatas:
Destrukturizuojame:
let Student = student => {
let {name, university} = student;
console.log(`${name} from ${university}`);
}
Student({
name: 'Vytautas',
university: 'KTU'
});
Rezultatas:
Galime destruktorių dėti ir į parametrus.
let Student = ({name, university}) => {
console.log(`${name} from ${university}`);
}
Student({
name: 'Vytautas',
university: 'KTU'
});
Rezultatas:
let thingsToDo ={
morning : 'Exercise',
afternoon: 'Work',
evening: 'Code',
night: ['Sleep', 'Dream']
};
let {morning, afternoon} = thingsToDo;
morning = 'Run';
console.log(morning, ' - ', afternoon);
Rezultatas:
Kitas pvz:
let Student = student => {
console.log(`${student.name} from ${student.university}`);
}
Student({
name: 'Vytautas',
university: 'KTU'
});
Rezultatas:
Destrukturizuojame:
let Student = student => {
let {name, university} = student;
console.log(`${name} from ${university}`);
}
Student({
name: 'Vytautas',
university: 'KTU'
});
Rezultatas:
Galime destruktorių dėti ir į parametrus.
let Student = ({name, university}) => {
console.log(`${name} from ${university}`);
}
Student({
name: 'Vytautas',
university: 'KTU'
});
Rezultatas:
Komentarai
Rašyti komentarą