"THIS", Apply, Call Flashcards

1
Q

what is “this”

A

the object that is executing the current function

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

if the function is part of an object we call it a _____

A

method

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

if the function is part of an object “THIS” references _____

A

itself

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

If the function is a regular function and not part of an object “THIS” references _______

A

global object

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

global object is_______ object in browsers & _______ in node

A

window

global

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

this. table = “windowtable”;
console. log(__________);

//Call on a GLOBAL scale

A

window.table

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

this.garage = {
table: “junk”;
}
//Call on a GLOBAL scale

A

this. garage.table

window. garage.table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
let stevesRoom = {
 table: "steves table"
};
console.log(this.stevesRoom.table);
// returns?
A

syntax error

“this” refers to global object

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

this.garage = {
table: “junk”,
cleanTable(){
console.log(cleaing ${this.table})
}
}

this.garage.table(); // returns

A

type error

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

this.table = ‘window table’;

const cleanTable = function (){
    console.log(`cleaing ${this.table}`)
};
cleanTable();
A

type error

“this” doesn’t have access to global property.

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

The _______method calls a function with a given this value, and arguments provided as an array (or an array-like object).

A

apply()

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

The _____ method calls a function with a given this value and arguments provided individually.

A

call()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
function Product(name, price) {
  this.name = name;
  this.price = price;
}
function Food(name, price) {
\_\_\_\_\_\_\_\_\_\_(this, name, price);
  this.category = 'food';
}
console.log(new Food('cheese', 5).name);
// expected output: "cheese"
A

product.call

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
const cleanTable = function (soap){
    console.log(`cleaing ${this.table} using ${soap}`)
};
cleanTable();

this.garage = {
table: “junk”,
}
//use call to output;
// cleaing junk using some soap

A

cleanTable.call(this.garage, ‘some soap’);

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

The ______ method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

A

bind()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
var module = {
  x: 42,
  getX: function() {
    return this.x;
  }
}
var unboundGetX = module.getX;
console.log(unboundGetX());
// expected output:
A

undefined

17
Q
var module = {
  x: 42,
  getX: function() {
    return this.x;
  }
}
var boundGetX = unboundGetX.bind(module);
console.log(boundGetX());
// expected output:
A

42

18
Q

HOW TO FIX THIS

const cleanTable = function (soap){
const innerFunction = function(_soap){
console.log(cleaing ${this.table} using ${soap})
}
innerFunction(soap);
};
cleanTable();

A

use arrow function

arrow function uses

19
Q

_______ & ________ use the “THIS” keyword

A
class
constructor
20
Q

when creating a function method in class you omit the __________key word

A

function

21
Q

var array = [‘a’, ‘b’];
var elements = [0, 1, 2];
array.push.apply(array, elements);
console.info(array); //

A

[“a”, “b”, 0, 1, 2]`