Execution Context Flashcards

1
Q

What is the global object?

A

JavaScript creates a global object when it starts running. It serves as theimplicit execution contextfor function invocations. In Node.js, the global object is the object namedglobal; in the browser, it’s thewindowobject. Global values such asInfinityandNaN, and global functions, such asisNaNandparseInt are properties of the global object.

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

What happens if you don’t use var, const, or let?

A

whenever you assign a value to a variable without using thelet,const, orvarkeywords (we’ll discussvarlater), the variable gets added to the global object as a property.

foo = ‘bar’;
global.foo; // => ‘bar’ (in Node)
window.foo; // => ‘bar’ (in a browser)

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

what happens if you don’t use var, const, or let in strict mode?

A

you get a reference error when assigning to the variable since it does not exist

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

What is execution context? How is it determined?

A

The execution context is a concept that refers to theenvironmentin which a function executes. In JavaScript, it most commonly refers to the current value of thethiskeyword. The only factor that determines the context is how you call the function or method.

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

What is the implicit execution context of a regular function call?

A
  • Within a regular function call (e.g.,foo()), JavaScript sets the binding forthisto the global object.
    • (Remember: in Node, the global object is calledglobal; in a browser, it iswindow.)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is the implicit execution context of a regular function call in strict mode?

A

when strict mode is enabled, the implicitthisis assigned toundefinedinstead of the global object

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

What is the implicit exception context if you invoke a function with parentheses?

A

when you invoke a function with parentheses, JavaScript uses the global object as the implicit context

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

What is the implicit context when you call a function with ‘new’?

A

When you call a function withnew, its implicit context is the new object

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

What is the implicit context for methods?

A

when you call a method that belongs to an object, the execution context inside that method call is the object used to call the method

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

how do we supply an explicit execution context

A

With .call() or .apply()

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

How do you use .call()?

A

function logNum() {
console.log(this.num);
}

let obj = {
num: 42
};

logNum.call(obj); // logs 42

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

how do you use .call() on a method?

A

let obj1 = {
logNum() {
console.log(this.num);
}
};

let obj2 = {
num: 42
};

obj1.logNum.call(obj2); // logs 42

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

how do you use call on a method that takes arguments?

A

someObject.someMethod.call(context, arg1, arg2, arg3, …)

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

Whats the difference between call and apply?

A

The only difference is thatapplyuses an array to pass any arguments to the function:

someObject.someMethod.apply(context, [arg1, arg2, arg3, …])

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

How do you hard bind a context to a function?

A

using .bind():
function sumNum(num1) {
return this.num + num1;
}

let obj = {
num: 42
};

let sumNum2 = sumNum.bind(obj);
sumNum2(5); // => 47

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

What does bind return?

A

bindreturns a new function. The new function ispermanentlybound to the object passed asbind’s first argument.

17
Q

How do we end up loosing context? 3 ways

A

There are 3 main ways:
1. Method copied from an object. when you take a method out of an object and execute it as a function or as a method on another object, the function’s context is no longer the original object.

  1. Inner Function Not Using the Surrounding Context.
    let obj = {
    a: ‘hello’,
    b: ‘world’,
    foo: function() {
    function bar() {
    console.log(this.a + ‘ ‘ + this.b);
    }bar();
    },
    };

obj.foo(); // => undefined undefined

  1. Function as Argument Losing Surrounding Context.
    Passing a function as an argument to another function strips it of its execution context, which means the function argument gets invoked with the context set to the global object.

let obj = {
a: ‘hello’,
b: ‘world’,
foo: function() {
[1, 2, 3].forEach(function(number) {
console.log(String(number) + ‘ ‘ + this.a + ‘ ‘ + this.b);
});
},
};

obj.foo();

// => 1 undefined undefined
// => 2 undefined undefined
// => 3 undefined undefined

18
Q

What happens if you use call or apply on a function returned from .bind()?

A

nothing. the execution context is still the context provided by bind.

19
Q

How do we preserve context if we copy a method from an object?

let john = {
firstName: ‘John’,
lastName: ‘Doe’,
greetings() {
console.log(‘hello, ‘ + this.firstName + ‘ ‘ + this.lastName);
},
};

john.greetings(); // context is john
let foo = john.greetings; // Strips context
foo(); // context is now the global object

A

We can:
1. pass a separate context object to our function:
function repeatThreeTimes(func, context) {
func.call(context);
func.call(context);
func.call(context);
}

function foo() {
let john = {
firstName: ‘John’,
lastName: ‘Doe’,
greetings: function() {
console.log(‘hello, ‘ + this.firstName + ‘ ‘ + this.lastName);
},
};

repeatThreeTimes(john.greetings, john);
}

foo();

// hello, John Doe
// hello, John Doe
// hello, John Doe

  1. We can hard bind the function:
    function repeatThreeTimes(func) {
    func();
    func();
    func();
    }

function foo() {
let john = {
firstName: ‘John’,
lastName: ‘Doe’,
greetings: function() {
console.log(‘hello, ‘ + this.firstName + ‘ ‘ + this.lastName);
},
};

repeatThreeTimes(john.greetings.bind(john));
}

foo();

// => hello, John Doe
// => hello, John Doe
// => hello, John Doe

20
Q

How do we preserve context if an inner function is not using the surrounding context?

let obj = {
a: ‘hello’,
b: ‘world’,
foo: function() {
function bar() {
console.log(this.a + ‘ ‘ + this.b);
}

bar();   }, };

obj.foo(); // => undefined undefined

A

We can:
1. Preserve Context with a Variable in Outer Scope:
ie let self = this
self.property

  1. Call the inner function with explicit context:
    foo: function() {
    function bar() {
    console.log(this.a + ‘ ‘ + this.b);
    }bar.call(this);
    },
  2. Use bind():
    foo: function() {
    let bar = function() {
    console.log(this.a + ‘ ‘ + this.b);
    }.bind(this);// some code
    bar();
    }
  3. Using an Arrow Function
    foo: function() {
    let bar = () => {
    console.log(this.a + ‘ ‘ + this.b);
    }// some code
    bar();
    }
21
Q

How do we prevent context loss when using a function as an argument?

foo: function() {
[1, 2, 3].forEach(function(number) {
console.log(String(number) + ‘ ‘ + this.a + ‘ ‘ + this.b);
});
},
// => 1 undefined undefined
// => 2 undefined undefined
// => 3 undefined undefined

A
  1. Preserve the Context with a Variable in Outer Scope:
    let self = this;
    [1, 2, 3].forEach(function(number) {
    console.log(String(number) + ‘ ‘ + self.a + ‘ ‘ + self.b);
    });
  2. Use Bind()
    foo: function() {
    [1, 2, 3].forEach(function(number) {
    console.log(String(number) + ‘ ‘ + this.a + ‘ ‘ + this.b);
    }.bind(this));
    },
  3. Use an arrow function:
    foo: function() {
    [1, 2, 3].forEach(number => {
    console.log(String(number) + ‘ ‘ + this.a + ‘ ‘ + this.b);
    });
    },
  4. Use the optional thisArg argument:
    foo: function() {
    [1, 2, 3].forEach(function(number) {
    console.log(String(number) + ‘ ‘ + this.a + ‘ ‘ + this.b);
    }, this);
    },
22
Q

What does it mean that JavaScript has first-class functions?

A
  • first-class functions that have the following characteristics:
    • You can add them to objects and execute them in the respective object’s context.
    • You can remove them from their objects, pass them around, and execute them in entirely different contexts.
    • They’re initially unbound but dynamically bound to a context object at execution time.
23
Q

What is the binding of arrow functions?

A

Arrow functions are always bound to the execution context of the enclosing function invocation. When defined at the top level, the context of an arrow function is the global object.