Chapter 8: Functions Flashcards
(20 cards)
Example function
// Print the name and value of each property of o. Return
undefined.
function printprops(o) {
for(let p in o) {
console.log(${p}: ${o[p]}\n
);
Function expression
/ This function expression defines a function that squares
its argument.
// Note that we assign it to a variable
const square = function(x) { return xx; };
// Function expressions can include names, which is useful
for recursion.
const f = function fact(x) { if (x <= 1) return 1; else
return xfact(x-1); };
Arrow function example
const sum = (x, y) => x + y;
Furthermore, if an arrow function has exactly one parameter, you can
omit the parentheses around the _______ ____
parameter list
const polynomial = x => xx + 2x + 3;
Arrow functions differ from functions defined in other ways in one
critical way: they inherit the value of the ____ keyword from the
environment in which they are defined rather than defining their own
invocation context as functions defined in other ways do
this
In JavaScript, functions may be nested within other functions
True
Functions written to be invoked as functions (and not as methods) do
not typically use the ____ keyword at all.
this
Other examples of method invocation
customer.surname.toUpperCase(); // Invoke method on
customer.surname
f().m(); // Invoke method m() on
return value of f()
Other examples of method invocation
If a function or method invocation is preceded by the keyword _____,
then it is a constructor invocation
new
JavaScript method parameters have no ______ _____, and no type
checking is performed on the values you pass to a function
declared types
In JavaScript,
however, functions are not only syntax but also values, which means
they can be assigned to variables, stored in the properties of objects or
the elements of arrays, passed as arguments to functions
True
Functions can also be assigned to ____ properties rather than
variables. As we’ve already discussed, we call the functions “methods”
when we do this
object
let o = {square: function(x) { return x*x; }}; // An object
literal
let y = o.square(16);
JavaScript
functions are executed using the ______ they were defined in. The
nested function f() was defined in a scope where the variable scope
was bound to the value “local scope”. That binding is still in effect
when f is executed, no matter where it is executed from
scope
let scope = “global scope”; // A global variable
function checkscope() {
let scope = “local scope”; // A local variable
function f() { return scope; } // Return the value in
scope here
return f;
}
let s = checkscope()(); // What does this
return?
“local scope”
// Return an array of functions that return the values 0-9
function constfuncs() {
let funcs = [];
for(var i = 0; i < 10; i++) {
funcs[i] = () => i;
}
return funcs;
}
let funcs = constfuncs();
funcs5 // => 10; Why doesn’t this return 5?
Returns 10 because the value of variable i at the end of the for loop is 10 so all 10 closures share this value
All functions, except arrow functions, have a _______ property
that refers to an object known as the prototype object
prototype
The primary purpose of _____ is to bind a function to an object
bind()
Function constructor
const f = new Function(“x”, “y”, “return x*y;”);
The ______ function in JavaScript is used to create a new function with a specific this value and optional preset arguments
It’s commonly used when you want to control what this refers to
bind()