Functions Flashcards

1
Q

How do you define default parameter values?

A
function multiply(a, b = 1) {
  return a * b;
}
multiply(5, 1); // 5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a ‘rest’ parameter and how do you define it?

A

A function’s last parameter can be prefixed with three dots which will cause all the remaining user-supplied arguments to be placed with in an array.

function myFun(a, b, ...moreArgs) {
  console.log('a', a);
  console.log('b', b);
  console.log('moreArgs', moreArgs);
}
myFun('one', 'two', 'three', 'four', 'five');
// a one
// b two
// moreArgs [ 'three', 'four', 'five' ]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s the difference between ‘rest’ parameters and the ‘spread’ operator?

A

Rest syntax looks like spread syntax, but in a way is opposite of spread. Spread expands an array into its elements, while rest collects multiple elements and condenses them into a single array.

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

How do you define an object property to a function that will be called when the property is looked up. For example, how to define the fahrenheit property so that it executes code to read/write to celsius internally?

const temp = new Temperature(22);

console. log(temp.fahrenheit); // 71.6
temp. fahrenheit = 86;
console. log(temp.celsius); // 30

A

Use getter/setters.

class Temperature {
  constructor(celsius) {
    this.celsius = celsius;
  }
  get fahrenheit() {
    return this.celsius * 1.8 + 32;
  } 
  set fahrenheit() {
    this.celsius = (value - 32) / 1.8;
  } 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Name four ways to create a function.

A

(1) Function declaration

function isEven(num) {
  return num % 2 === 0;
}

(2) Function expression

const count = function(x) {
  return x.length;
}

(3) Shorthand method definition

const foo = {
  shortMethodDef(x) {
    console.log(x);
  },
  traditionalMethodDef: function(x) {
    console.log(x);
  }
}

(4) Arrow function

cons absValue = (number) => {
  if (number < 0) {
    return -number;
  }
  return number;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Are function declarations hoisted?

A

Yes, function declarations are hoisted up to the top of the current scope, which means they can be invoked before the declaration.

hoisted(); // logs ‘bar’

function hoisted() {
  console.log('bar');
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Are function expressions hoisted?

A

No, function expressions are not hoisted.

notHoisted(); // TypeError: notHoisted is not a function
var notHoisted = function() {
  console.log('bar');
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a method?

A

A method is a function associated with an object, or to put it another way, a method is a property of an object that is a function.

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

How do you define an arrow function?

A

An arrow function is defined with the list of parameters followed by a fat arrow followed by the function body.

const absValue = (number) => {
  if (number < 0) {
    return -number;
  } 
  return number;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Will ‘age’ get incremented here?

function Person() {
  this.age = 0;

setInterval(() => {
this.age++;
}, 1000);
}

A

Yes, because an arrow function does not have its own ‘this’. The ‘this’ value of the enclosing lexical scope is used.

Here, the ‘this’ properly refers to the Person object.

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

Will ‘age’ get incremented here?

function Person() {
  this.age = 0;

setInterval(function growUp() => {
this.age++;
}, 1000);
}

A

No, here ‘this’ refers to the global object so the growUp function won’t increment the age of Person.

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