Functions Flashcards
Functions that are part of objects are called ________.
methods
function hello(name = ‘Chris’) {
console.log(Hello ${name}!
);
}
hello(‘Ari’); returns?
hello(); returns
// Hello Ari!
// Hello Chris!
Unlike function declaration, function expressions are ___________
not hoisted
Unlike function declaration, ___________ are not hoisted
function expressions
if your arrow function needs to return a value, and contains only one line, you can also omit the__________.
return statement
Arrow functions don’t have their own bindings to ____ , ______ ,______ and should not be used as methods.
this, arguments, or super,
Arrow functions cannot be used as _________. Calling them with new throws a _________ . T
constructors
TypeError
Arrow functions cannot use ________ within their body and cannot be created as generator functions.
Yield
// Traditional anonymous function
(function (a) {
return a + 100;
});
write as arrow
a => a+100;
// Traditional anonymous function
(function (a, b) {
return a + b + 100;
});
write as arrow
(a, b) => a + b + 100;
// Traditional anonymous function (no parameters)
(function () {
return a + b + 100;
});
write as arrow
() => a + b + 100;
// Traditional anonymous function
(function (a, b) {
const chuck = 42;
return a + b + chuck;
});
write as arrow
// Arrow function
(a, b) => {
const chuck = 42;
return a + b + chuck;
};
If the arrow function needs to call itself, use a named ____________ instead.
function expression
// Traditional Function
function bob(a) {
return a + 100;
}
write as arrow
const bob2 = (a) => a + 100;
const func2 = (x, y) => {
};
what is missing
return x + y;
In a concise body, only a single expression is specified, which becomes the implicit return value. In a block body, you must use an explicit return statement.
const func = () => { foo: 1 };
// Calling func() returns undefined!
how to fix?
const func = () => ({ foo: 1 });
For similar reasons, the_______, _______, _______methods are not useful when called on arrow functions, because arrow functions establish this based on the scope the arrow function is defined within, and the this value does not change based on how the function is invoked.
call(), apply(), and bind()
function showMessage() {
alert( ‘Hello everyone!’ );
}
function expression or function declaration.?
function declaration.
let userName = ‘John’;
function showMessage() {
let message = ‘Hello, ‘ + userName;
alert(message);
}
showMessage(); returns?
// Hello, John
let userName = ‘John’;
function showMessage() {
userName = “Bob”; /
let message = ‘Hello, ‘ + userName;
alert(message);
}
alert( userName );
showMessage();
alert( userName );
// John before the function call
// Bob, the value was modified by the function
let userName = ‘John’;
function showMessage() {
let userName = “Bob”;
let message = ‘Hello, ‘ + userName;
alert(message);
}
showMessage();
alert( userName );
// John, unchanged, the function did not access the outer variable
Variables declared outside of any function, such as the outer userName in the code above, are called ______
global.
If a function is called, but an argument is not provided, then the corresponding value becomes ________.
undefined
We can specify the so-called “default” (to use if omitted) value for a parameter in the function declaration, using _______:
=
function showMessage(from, _______) {
alert( from + “: “ + text );
}
showMessage(“Ann”);
what to write if you want default value “no text given”
function showMessage(from, text = “no text given”) {
alert( from + “: “ + text );
}
function showMessage(from, text) {
if (//_____ insert here) {
text = ‘no text given’;
}
insert text to check if undfined
function showMessage(from, text) {
if (text === undefined) {
text = ‘no text given’;
}