“JavaScript Destructuring” Kode Jawaban

Objek Menghancurkan

const book = {
    title: 'Ego is the Enemy',
    author: 'Ryan Holiday',
    publisher: {
        name: 'Penguin',
        type: 'private'
    }
};

const {title: bookName =  'Ego', author, name: {publisher: { name }} = book, type: {publisher: { type }} = book } = book;
Scary Snail

Objek Menghancurkan

Object Destructuring =>
//
   The destructuring assignment syntax is a JavaScript expression that makes it
possible to unpack values from arrays,
or properties from objects, into distinct variables.
//
example:
const user = {
    id: 42,
    is_verified: true
};

const {id, is_verified} = user;

console.log(id); // 42
console.log(is_verified); // true 
kepl3r

merusak

var foo = ['one', 'two', 'three'];

// without destructuring
var one   = foo[0];
var two   = foo[1];
var three = foo[2];

// with destructuring
var [one, two, three] = foo;
Lovely Lion

Contoh Destrukturisasi Objek

const hero = {
  name: 'Batman',
  realName: 'Bruce Wayne',
  address: {
    city: 'Gotham'
  }
};

// Object destructuring:
const { realName, address: { city } } = hero;
city; // => 'Gotham'
Nutty Narwhal

JavaScript array destruktur

const arrValue = ['one', 'two', 'three'];

// destructuring assignment in arrays
const [x, y, z] = arrValue;

console.log(x); // one
console.log(y); // two
console.log(z); // three
SAMER SAEID

JavaScript Destructuring

// before you would do something like this
const person = {
    name: 'Sara',
    age: 25,
    gender: 'female'    
}

let name = person.name;
let age = person.age;
let gender = person.gender;

console.log(name); // Sara
console.log(age); // 25
console.log(gender); // female
SAMER SAEID

Jawaban yang mirip dengan “JavaScript Destructuring”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya