Chapter 8: Functions Flashcards

(20 cards)

1
Q

Example function

A

// 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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Function expression

A

/ 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 x
fact(x-1); };

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Arrow function example

A

const sum = (x, y) => x + y;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Furthermore, if an arrow function has exactly one parameter, you can
omit the parentheses around the _______ ____

A

parameter list

const polynomial = x => xx + 2x + 3;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

this

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In JavaScript, functions may be nested within other functions

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Functions written to be invoked as functions (and not as methods) do
not typically use the ____ keyword at all.

A

this

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

o“m”; // Another way to write o.m(x,y).
a0 // Also a method invocation (assuming a[0] is
a function).

A

Other examples of method invocation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

customer.surname.toUpperCase(); // Invoke method on
customer.surname
f().m(); // Invoke method m() on
return value of f()

A

Other examples of method invocation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

If a function or method invocation is preceded by the keyword _____,
then it is a constructor invocation

A

new

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

JavaScript method parameters have no ______ _____, and no type
checking is performed on the values you pass to a function

A

declared types

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Functions can also be assigned to ____ properties rather than
variables. As we’ve already discussed, we call the functions “methods”
when we do this

A

object

let o = {square: function(x) { return x*x; }}; // An object
literal
let y = o.square(16);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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?

A

“local scope”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

// 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?

A

Returns 10 because the value of variable i at the end of the for loop is 10 so all 10 closures share this value

17
Q

All functions, except arrow functions, have a _______ property
that refers to an object known as the prototype object

18
Q

The primary purpose of _____ is to bind a function to an object

19
Q

Function constructor

A

const f = new Function(“x”, “y”, “return x*y;”);

20
Q

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