Ad. Javascript.info part 5 Flashcards

1
Q

function obj f

time of function execution:
time of “this” binding:

A

future

future

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
function call 
f()

time of function execution:
time of “this” binding:

A

now

now

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

f.call()

time of function execution:
time of “this” binding:

A

now

now

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

f.apply()

time of function execution:
time of “this” binding:

A

now

now

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

f.bind()

time of function execution:
time of “this” binding:

A

future

now

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

CALL(), APPLY, BIND() attach ________ into function (or object) and the difference is in the function invocation (see below).

A

“this”

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

CALL / APPLY / BIND?

var person1 = {firstName: 'Jon', lastName: 'Kuperman'};
var person2 = {firstName: 'Kelly', lastName: 'King'};
function say(greeting) {
    console.log(greeting + ' ' + this.firstName + ' ' + this.lastName);
}

say. ___(person1, ‘Hello’); // Hello Jon Kuperman
say. ___(person2, ‘Hello’); // Hello Kelly King

A

CALL

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

CALL / APPLY / BIND?

var person1 = {firstName: 'Jon', lastName: 'Kuperman'};
var person2 = {firstName: 'Kelly', lastName: 'King'};
function say(greeting) {
    console.log(greeting + ' ' + this.firstName + ' ' + this.lastName);
}

say. ___(person1, [‘Hello’]); // Hello Jon Kuperman
say. ___(person2, [‘Hello’]); // Hello Kelly King

A

APPLY

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
var person1 = {firstName: 'Jon', lastName: 'Kuperman'};
var person2 = {firstName: 'Kelly', lastName: 'King'};
function say() {
    console.log('Hello ' + this.firstName + ' ' + this.lastName);
}
var sayHelloJon = say.\_\_\_(person1);
var sayHelloKelly = say.\_\_\_(person2);

sayHelloJon(); // Hello Jon Kuperman
sayHelloKelly(); // Hello Kelly King

A

BIND

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

_____ and ________ are pretty interchangeable. Just decide whether it’s easier to send in an array or a comma separated list of arguments.

A

Call and apply are pretty interchangeable. Just decide whether it’s easier to send in an array or a comma separated list of arguments.

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

__________ is a bit different as it returns a new function

A

bind()

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

GOOD OR BAD PRACTICE?

function sayHello() {
    console.log("hello");
}

Object.prototype.hello = sayHello;
Date.hello(); // Prints hello

A

BAD

2

The reason you don’t want to use Object.prototype.sayHi = function(){} is that once you do, everything that has Object in its prototype chain will be able to use sayHi. That’s the core of prototypical inheritance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
function sayHello() {
    console.log("hello");
}

Object.prototype.hello = sayHello;
Date.hello(); //

A

Prints hello

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

Function._______ and Function._______ use whatever function you are calling or applying at the moment of the call or apply.

A

call

apply

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
function say() {
    console.log(arguments);
}
say.\_\_\_\_\_\_(this, "a", "b");
say.\_\_\_\_\_\_\_(this, ["a", "b"]);
A

call

apply

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
function honk() {
    console.log(this.sound);
}
function Car() {
    this.sound = "honk";
}
function Van(){
    this.sound = "beep";
}
var c = new Car();
var v = new Van();
var ftorCar = \_\_\_\_\_\_\_\_
var ftorVan = \_\_\_\_\_\_\_\_

ftorCar(); // prints honk
ftorVan(); // prints beep

A

honk. bind(c);

honk. bind(v);

17
Q
function f() {
  alert(this.name);
}

f = f.bind( {name: “John”} ).bind( {name: “Ann” } );

f(); //returns

A

John

The exotic bound function object returned by f.bind(…) remembers the context (and arguments if provided) only at creation time.

A function cannot be re-bound.

18
Q

As we remember from the chapter Object methods, “this”, arrow functions do not have “______”.

A

this

19
Q

arrow functions take this from ______

A

from the outside.

20
Q

let group = {

title: “Our Group”,
students: [“John”, “Pete”, “Alice”],

  showList() {
    this.students.forEach(
      student => alert(this.title + ': ' + student)
    );
  }
};

group.showList(); // returns?

A

“John”, “Pete”, “Alice”]

21
Q

let group = {

title: “Our Group”,
students: [“John”, “Pete”, “Alice”],

  showList() {
    this.students.forEach(function(student) {
      alert(this.title + ': ' + student)
    });
  }
};

group.showList();

A

error

22
Q

Not having this naturally means another limitation: arrow functions can’t be used as _____ They can’t be called with new.

A

consturctors