Variable and argument destructuring

This is the last piece that you'll need to understand, as it tends to appear in a lot of JavaScript code tied to Phoenix channels. This last concept is centered around destructuring variables and arguments and it actually works a lot like what you're used to in Elixir. It basically allows you to do things like this:

const person = {
firstName: "Jane",
lastName: "Smith",
age: 30
};

const { firstName, lastName, age } = person;

console.log(firstName); // Will output "Jane"
console.log(lastName); // Will output "Smith"
console.log(age); // Will output "30"

This is a nice, cleaner way of doing something that previously required as much code as this:

const person = {
firstName: "Jane",
lastName: "Smith",
age: 30
};

const firstName = person.firstName;
const lastName = person.lastName;
const age = person.age;

console.log(firstName); // Will output "Jane"
console.log(lastName); // Will output "Smith"
console.log(age); // Will output "30"

It can also be used in functions, as well:

const greeting = { firstName, lastName } => "Hello " + firstName + " " + lastName + "!";

This does the same thing as the other destructuring code we wrote earlier! This should be enough ES2015 for you to be able to understand everything you might need as we work through the JavaScript code that we'll need to write to finish implementing the channels for our application!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset