7.Arrow Functions in Javascript - Passing Objects to Functions - Passing Array to Functions

preview_player
Показать описание
by the end of this videos YOu will be able to
1.Write Arrow functions
2.Pass objects to a function
3.Pass array to a function
Just as we can have destructured assignment (see Chapter 5), we can have destruc‐
tured arguments (arguments are, after all, very much like variable definition). Con‐
sider destructuring an object into individual variables:
function getSentence({ subject, verb, object }) {
return `${subject} ${verb} ${object}`;
}
const o = {
subject: "I",
verb: "love",
object: "JavaScript",
};
getSentence(o); // "I love JavaScript"
As with destructuring assignment, property names must be identifier strings, and a
variable that doesn’t have a matching property in the incoming object will receive the
value undefined.
You can also destructure arrays:
function getSentence([ subject, verb, object ]) {
return `${subject} ${verb} ${object}`;
}
const arr = [ "I", "love", "JavaScript" ];
getSentence(arr);

ES6 introduces a new and welcome syntax called arrow notation (also called “fat
arrow” notation because the arrow uses an equals sign instead of a dash). It is essen‐
tially syntactic sugar (with one major functional difference we’ll get to shortly) that
reduces the number of times you have to type the word function, as well as the num‐
ber of curly braces you have to type.
Arrow functions allow you to simplify syntax in three ways:
• You can omit the word function.
• If the function takes a single argument, you can omit the parentheses.
• If the function body is a single expression, you can omit curly braces and the
return statement.
Arrow functions are always anonymous. You can still assign them to a variable, but
you can’t create a named function like you can with the function keyword.
Рекомендации по теме
Комментарии
Автор

love u bro, helped a lot, big hug from brazil <3

guilhermesouza
Автор

so the whenever I see curly braces like {} around a parameter or set of parameters going into a function they are just a selected set of properties from another object that we want the function to be able to use?

VividElites
Автор

If values inside the array is not fixed what should we do ?
Or can you explain if there is complex variable like objects in the array then how the code will work

fanaticbark
Автор

"return ..;"? Why do people insist on trying to teach programming when they don't even know how to write code???

atlantic_love
visit shbcf.ru