This and Object Prototypess Flashcards

1
Q

What does this do?

A

This mechanism provides a more elegant way of implicitly “passing along” an object reference, leading to cleaner API design and easier re-use.

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

Two misconceptions about this

A

Itself: it is assumed this refers to the function itself.

Its Scope: this is that it somehow refers to the function’s scope

both are wrong

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

is this runtime or author-time binding

A

runtime:
this binding has nothing to do with where a function is declared, but this is actually a binding that is made when a function is invoked, and what it references is determined entirely by the call-site where the function is called.

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

what is a function call-site

A

the location in code where a function is called (not where it’s declared).

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

what is a function call-stack

A

the stack of functions that have been called to get us to the current moment in execution

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
Where are the call sites and call stacks of functions below?
function baz() {
    console.log( "baz" );
    bar();
}
function bar() {
    console.log( "bar" );
}

baz();

A
function baz() {
    // call-stack is: `baz`
    // so, our call-site is in the global scope
    console.log( "baz" );
    bar(); //  `bar`
    // so, our call-site is in `baz`
console.log( "bar" ); }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

why do we care about finding the call-site?

A

we care about finding the actual call-site (from the call-stack), because it’s the only thing that matters for this binding.

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

4 rules for this

A

Default Binding
Implicit Binding
Explicit Binding
new Binding

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

Default Binding

A

default catch-all rule when none of the other rules apply
function foo() {
console.log( this.a );
}
var a = 2;
foo(); // 2
foo() is called, this.a resolves to our global variable a. Because in this case, the default binding for this applies to the function call, and so points this at the global object. foo() would render undefined in strict mood

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
Implicit Binding
function foo() {
	console.log( this.a );
}
var obj2 = {
	a: 42,
	foo: foo
};
var obj1 = {
	a: 2,
	obj2: obj2
};
obj1.obj2.foo(); // 42
A

does the call-site have a context object, also referred to as an owning or containing object.

it’s that object which should be used for the function call’s this binding.
Because obj is the this for the foo() call, this.a is synonymous with obj.a

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

Explicit Binding

A
using call() or apply(): Invoking foo with explicit binding by foo.call(..) allows us to force its this to be obj
function foo() {
	console.log( this.a );
}
var obj = {
	a: 2
};
foo.call( obj ); // 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

new Binding

function foo(a) {
	this.a = a;
}
var bar = new foo( 2 );
console.log( bar.a ); // 2
A

New key word:

1. a brand new object is created (aka, constructed) out of thin air
2. the newly constructed object is [[Prototype]]-linked
3. the newly constructed object is set as the this binding for that function call
4. unless the function returns its own alternate object, the new-invoked function call will automatically return the newly constructed object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

two forms of objects

A
the declarative (literal) form, and the constructed form
var myObj = {key: value}; 
constructed:
var myObj = new Object();
myObj.key = value;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

6 primary types in Javascript

A
string
    number
    boolean
    null
    undefined
    object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

is null an object

A

no, null is its own primitive type.

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