Prototypes, objects and functions Flashcards

1
Q

Ways to set a prototype (3 ways)

A

let b = Object.create(a);
(will make a a prototype of b)

Object.setPrototypeOf(b, a)

let c = new ConstructorFunction()

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

if A is a prototype of B, and A has the key ‘apple’, what is return from
B.hasOwnProperty(‘apple’)

A

false

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

Return the prototpe of any given object

A

> Object.getPrototypeOf(b)

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

a for in loop iterates over all the properties of the prototype as well. How can you make this not happen?

A

Have a guard clause involving hasOwnProperty

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

When you iterate over an object, why doesn’t it iterate over all the default methods of the default prototype.

A

They are not enumerable
Object.prototype.propertyIsEnumerable

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

What does enumerable mean?

A

means they can be iterated over

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

What happens
let a = {
foo: 1,
};

let b = {
bar: 2,
};

let c = {
baz: 3,
};

Object.setPrototypeOf(c, b);
Object.setPrototypeOf(b, a);

console.log(c.bar);
console.log(c.foo);

A

// => 2
// => 1

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

What is the prototype of Object.prototype

A

null

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

What does this do?
https://launchschool.com/lessons/1eaf5e37/assignments/ccf2e4a7
function Foo() {
this.bar = “qux”
}

Foo.prototype.baz = function() {};
let foo = new Foo();
console.log(foo.propertyIsEnumerable(‘bar’));
console.log(Object.getPrototypeOf(foo).propertyIsEnumerable(‘baz’));

A

// true
// true

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

How does dunder work
__proto__

A

It’s depracated, will have to learn later

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

T or F
when two objects in the same prototype chain have a property with the same name, the object that’s closer to the calling object takes precedence.

A

T

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

Create an object with no prototype

A

> let a = Object.create(null)
undefined

> Object.getPrototypeOf(a)
null

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

Check to see if an object is a prototype of another. But make sure it will never throw an error if one of those objects has a prototype of null.

A

if (Object.getPrototypeOf(obj) && obj.isPrototypeOf(car)) {
}

If you don’t first check whether obj has a non-null prototype, this code will raise an exception if obj has a null prototype.
BECAUSE
isPrototpeOf is a method on Object.prototype.
If the prototype is null, then there is no method to call

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

https://launchschool.com/lessons/1eaf5e37/assignments/f7b8620b Question 4 (2 ways, loop and recursive )
Write a function that searches the prototype chain of an object for a given property and assigns it a new value. If the property does not exist in any of the prototype objects, the function should do nothing. The following code should work as shown:

A

(2 ways)

function assignProperty(obj, property, value) {
while (obj !== null) {
if (obj.hasOwnProperty(property)) {
obj[property] = value;
break;
}

obj = Object.getPrototypeOf(obj);   } }

function assignProperty(obj, property, value) {
if (obj === null) { // property not found
return;
} else if (obj.hasOwnProperty(property)) {
obj[property] = value;
} else {
assignProperty(Object.getPrototypeOf(obj), property, value);
}
}

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

Have a named function expression as a callback function (rather than the usual arrow function)

A

let squaredNums = [1, 2, 3].map(function squareNum(num) {
return num * num;
});

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

What happens with this code?
let foo = function bar() {};
foo();
bar();

A

foo(); // This works
bar(); // This does not work

foo is a local variable that contains a reference to the function, so we can invoke the function using foo(). However, the function name, bar, is not in scope on line 3, so bar() does not work.

The function name on a function expression is visible inside the function, which is useful when working with recursive functions.

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

Is prompt an anonymous function?
let prompt = function() { // Assign to a variable

};

A

Promp is a variable assigned to an anonymous function.

if you had had
function prompt()
it is said to be named

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

Explain how this works:
function makeIncrementer(increment) {
return function(value) { // return to caller
return value + increment;
};
};

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

What is console.log(typeof (x => {}))

A

function

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

Rewrite this so we don’t have to have a parameter for the language everytime (set the language once at the top).

function greet(language) {
switch (language) {
case ‘en’:
console.log(‘Hello!’);
break;
case ‘es’:
console.log(‘Hola!’);
break;
case ‘fr’:
console.log(‘Bonjour!’);
}
}

greet(‘fr’); // logs ‘Bonjour!’

A

function createGreeter(language) {
switch (language) {
case ‘en’:
return () => console.log(‘Hello!’);
case ‘es’:
return () => console.log(‘Hola!’);
case ‘fr’:
return () => console.log(‘Bonjour!’);
}
}

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

What’s the difference between isNan (the global object method) and Number.isNaN?

A

Number.isNaN is looking for the number NaN

The global object isNaN coerces values to numbers. So it really answers “is this a number, or NaN”
Number.isNaN just checks if the value is the value NaN

Number.isNaN(‘I am not a number’); // false - this is a correct value
isNaN(‘I am not a number’); // true - string gets coerced to NaN

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

foo = ‘bar’;
foo; // => ‘bar’
foo is different from window.foo or global.foo
T or F

A

F

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

NaN, Infinity, and setTimeout are properties on the global object
T or F

A

T

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

What’s the term for:
Setting the execution context

A

binding “this”
or
setting the binding

25
Q

Objects can inherit properties and methods from another. What is this called?

A

prototypal inheritance

26
Q

What’s the object called that is inhereted from? (2 terms)

A

prototype
or
prototype object

27
Q

If you call a method or access a property from an object that doesn’t have it but it’s prototype does, what’s it called?

A

The object where access was attempted delegates the property and/or method access to its prototype

28
Q

the line of prototypal inheritence is called

A

prototype chain

29
Q

Difference between implicit and explicit execution context

A

Unless you set it explicitly, it’s probably implicit

30
Q

Explicitly invoke a function with a certain context

A

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

let obj = {
num: 42
};

logNum.call(obj); // logs 42

31
Q

Explicitly invoke a function with a certain context with arguments

A

obj.num = sumNum.call(obj, 5);
object, arguments

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

32
Q

Another way to explicitly invoke a function with a certain context. What’s the difference?

A

The apply method works in much the same way as call. The only difference is that apply uses an array to pass any arguments to the function. Here’s the general syntax:

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

But it’s not really needed because you can jsut do this:
someObject.someMethod.call(context, …args);

33
Q

Bind a function or method to a specific execution context

A

You can’t!
But you can create a new method or function and bind it.

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

let obj = {
num: 42
};

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

34
Q

Once bound, call the function in another specific execution context
Once bound, bind it to another object

A

Can’t do it
Can’t do it

35
Q

What’s the object context in which a function is executed referred to as?

A

Execution context

36
Q

Fix this (2 WAYS)
function repeatThreeTimes(func) {
func(); // can’t use func.call(john); john is out of scope
func();
func();
}

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

repeatThreeTimes(john.greetings); // Strips context
}

foo();

// => hello, undefined undefined
// => hello, undefined undefined
// => hello, undefined undefined

A

CONTEXT AS A PARAMETER
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

WITH BINDING
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

37
Q

Advantages and disadvantages to using bind

A

bind has one significant advantage: once you bind a context to a function, that binding is permanent and does not need to be repeated if it gets called more than once. The disadvantage of bind is that it is no longer possible to determine the context by looking at the invocation of the final function.

38
Q

Fix this: (4 ways)
https://launchschool.com/lessons/1eaf5e37/assignments/dc50753a

A

https://launchschool.com/lessons/1eaf5e37/assignments/dc50753a
4 solutions
1. Arrow function
2. .call or .apply
3. .bind
4. let self = this;

function bar() {
  console.log(self.a + ' ' + self.b);
}
39
Q

What defines an arrow function’s this

A

they use the execution context from the surrounding context in which they are defined.
That means that an arrow function defined inside another function always has the same context as the outer function’s execution context at the time the function is defined.
Using arrow functions like this is similar to using bind in that you don’t have to worry about arrow functions losing their surrounding context. An arrow function, once created, always has the same context as the function that surrounded it when it was created.

40
Q

Why does this happen

let obj = {
a: 5,

foo: () => {
console.log(this.a);
},
};

obj.foo(); // => undefined

A

If a function is called by an object reference then the value of this is implicitly set to that object, otherwise, the value of this is implicitly set to the global object or undefined.
So if this was a regular method, this would be set to obj.
But arrow functions do not follow this convention, and just adopt the execution context they are defined in. In this case, they were not defined in a function that references an object, so heir execution context is the global cobject.

41
Q

What happens with this:

let obj = {
apple: this,
};

console.log(obj.apple);

A

globalobject or window
But not in node/vsc

42
Q

What does it mean to get context lexically

A

“Lexical” refers to the structure of the surrounding code without regard to its execution state; arrow functions rely on the execution state at the time of definition to determine their execution context.

43
Q

What happens with this:
let obj1 = {
a: ‘This is obj1’,

foo() {
let bar = () => console.log(this.a);
bar();
},
};

let obj2 = {
a: ‘This is obj2’,
};

obj1.foo();

obj2.foo = obj1.foo;
obj2.foo();

A

obj1.foo(); // This is obj1

obj2.foo = obj1.foo;
obj2.foo(); // This is obj2

arrow functions rely on the execution state at the time of definition to determine their execution context.
Time of definition mind you

44
Q

let func = function(arg1) {
console.log(arg1);
};

What happens with:

func.bind(context)(‘1’)

func.bind(context, ‘1’)()

func.bind(context, ‘1’)(‘2’)

let popper = func.bind(context, ‘1’);
popper(‘2’);

A

‘1’
‘1’
‘1’
‘1’

45
Q

Describe what .bind() does/returns:

A

The bind() method of Function instances creates a new function that, when called, calls this function with its this keyword set to the provided value, and a given sequence of arguments preceding any provided when the new function is called.

46
Q

What happens with this code?
let car = {};

let obj = Object.create(null);

console.log(obj.isPrototypeOf(car));

A

error

Basically if null is either the object or the prototype it will throw an error.

47
Q

bind a function or method at declaration
Method and function

A

Method
let apple = {
always: function() {
console.log(this.a);
}.bind(obj)
}

function
let apple = function() {
console.log(this);
}.bind(obj);

48
Q

What happens with:
let obj = {
a: 1,
apple: function() {
console.log(this);
let pear = () => {
console.log(this);
let orange = () => {
console.log(this);
}
orange();
}
pear();
}
};

A

Logs obj 3 times

49
Q

Problems 1-3
https://launchschool.com/lessons/1eaf5e37/assignments/408c20c3

A

https://launchschool.com/lessons/1eaf5e37/assignments/408c20c3

50
Q

Problems 4-7
https://launchschool.com/lessons/1eaf5e37/assignments/408c20c3

The forEach method provides an alternative way to supply the execution context for the callback function. Modify the program from the previous problem to use that technique to produce the proper output.
(as well as a few other methods)

A

https://launchschool.com/lessons/1eaf5e37/assignments/408c20c3

51
Q

Problems 8-9
https://launchschool.com/lessons/1eaf5e37/assignments/408c20c3

A

https://launchschool.com/lessons/1eaf5e37/assignments/408c20c3

52
Q

The value of this changes based on how you define a function, not how you invoke it
T or F

A

F

53
Q

What happens with:
let car = null;
let obj = Object.create(car);
console.log(car.isPrototypeOf(obj));

A

Type Error

54
Q

Set the prototype of an object when creating itusing a constructor function

A

let DogPrototype = {
bark() {
console.log(this.weight > 20 ? ‘Woof!’ : ‘Yip!’);
}
};

function Dog(name, breed, weight) {
Object.setPrototypeOf(this, DogPrototype);
this.name = name;
this.breed = breed;
this.weight = weight;
// this.bark method removed.
}

55
Q

Write an if statement that only goes forward if an object is a prototype of another and the global object is in the prototype chain while preventing errors

How would you write it to test if an object is a prototype of another and null at the top of the prototypal chain

A

if (Object.getPrototypeOf(obj) && obj.isPrototypeOf(car)) {
}

Object.getPrototypeOf(obj1) ===obj2

56
Q

Set the execution context with forEach

A

.forEach( function() {
}, context)

like this:
const TESgames = {
titles: [‘Arena’, ‘Daggerfall’, ‘Morrowind’, ‘Oblivion’, ‘Skyrim’],
seriesTitle: ‘The Elder Scrolls’,
listGames: function() {
this.titles.forEach(function(title) {
console.log(this.seriesTitle + ‘: ‘ + title);
}, this);
}
};

TESgames.listGames();

57
Q

How to check is a property is enumerable

A

Object.prototype.propertyIsEnumerable(‘’)

58
Q

let obj1 = {
a: 1,
b: 2,
};

let obj2 = Object.create(obj1);

obj2.c = 3;
obj2.d = 4;

console.log(obj2.propertyIsEnumerable(‘b’));

A

false

59
Q

What does this do?
let apple = {yo: 123};

let purple = {
yo: 654,

speak: function(plusThis, andThis) {
console.log(this.yo + ‘ ‘ + plusThis + ‘ ‘ + andThis);
},
};

let poopoo = purple.speak.bind(apple, ‘a’);

poopoo(‘plusThis’, ‘andThis’);

A

123 a plusThis

It looks for unbound arguments within the function parameters from left to right regardless of other bound arguments