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

Показать описание
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.
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.
Комментарии