what is “this”
the object that is executing the current function
if the function is part of an object we call it a _____
method
if the function is part of an object “THIS” references _____
itself
If the function is a regular function and not part of an object “THIS” references _______
global object
global object is_______ object in browsers & _______ in node
window
global
this. table = “windowtable”;
console. log(__________);
//Call on a GLOBAL scale
window.table
this.garage = {
table: “junk”;
}
//Call on a GLOBAL scale
this. garage.table
window. garage.table
let stevesRoom = {
table: "steves table"
};
console.log(this.stevesRoom.table);
// returns?syntax error
“this” refers to global object
this.garage = {
table: “junk”,
cleanTable(){
console.log(cleaing ${this.table})
}
}
this.garage.table(); // returns
type error
this.table = ‘window table’;
const cleanTable = function (){
console.log(`cleaing ${this.table}`)
};
cleanTable();type error
“this” doesn’t have access to global property.
The _______method calls a function with a given this value, and arguments provided as an array (or an array-like object).
apply()
The _____ method calls a function with a given this value and arguments provided individually.
call()
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"product.call
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
cleanTable.call(this.garage, ‘some soap’);
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.
bind()
var module = {
x: 42,
getX: function() {
return this.x;
}
}
var unboundGetX = module.getX; console.log(unboundGetX()); // expected output:
undefined
var module = {
x: 42,
getX: function() {
return this.x;
}
}
var boundGetX = unboundGetX.bind(module); console.log(boundGetX()); // expected output:
42
HOW TO FIX THIS
const cleanTable = function (soap){
const innerFunction = function(_soap){
console.log(cleaing ${this.table} using ${soap})
}
innerFunction(soap);
};
cleanTable();
use arrow function
arrow function uses
_______ & ________ use the “THIS” keyword
class constructor
when creating a function method in class you omit the __________key word
function
var array = [‘a’, ‘b’];
var elements = [0, 1, 2];
array.push.apply(array, elements);
console.info(array); //
[“a”, “b”, 0, 1, 2]`