Prototypes, objects and functions Flashcards
Ways to set a prototype (3 ways)
let b = Object.create(a);
(will make a a prototype of b)
Object.setPrototypeOf(b, a)
let c = new ConstructorFunction()
if A is a prototype of B, and A has the key ‘apple’, what is return from
B.hasOwnProperty(‘apple’)
false
Return the prototpe of any given object
> Object.getPrototypeOf(b)
a for in loop iterates over all the properties of the prototype as well. How can you make this not happen?
Have a guard clause involving hasOwnProperty
When you iterate over an object, why doesn’t it iterate over all the default methods of the default prototype.
They are not enumerable
Object.prototype.propertyIsEnumerable
What does enumerable mean?
means they can be iterated over
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);
// => 2
// => 1
What is the prototype of Object.prototype
null
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’));
// true
// true
How does dunder work
__proto__
It’s depracated, will have to learn later
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.
T
Create an object with no prototype
> let a = Object.create(null)
undefined
> Object.getPrototypeOf(a)
null
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.
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
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:
(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);
}
}
Have a named function expression as a callback function (rather than the usual arrow function)
let squaredNums = [1, 2, 3].map(function squareNum(num) {
return num * num;
});
What happens with this code?
let foo = function bar() {};
foo();
bar();
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.
Is prompt an anonymous function?
let prompt = function() { // Assign to a variable
};
Promp is a variable assigned to an anonymous function.
if you had had
function prompt()
it is said to be named
Explain how this works:
function makeIncrementer(increment) {
return function(value) { // return to caller
return value + increment;
};
};
What is console.log(typeof (x => {}))
function
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!’
function createGreeter(language) {
switch (language) {
case ‘en’:
return () => console.log(‘Hello!’);
case ‘es’:
return () => console.log(‘Hola!’);
case ‘fr’:
return () => console.log(‘Bonjour!’);
}
}
What’s the difference between isNan (the global object method) and Number.isNaN?
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
foo = ‘bar’;
foo; // => ‘bar’
foo is different from window.foo or global.foo
T or F
F
NaN, Infinity, and setTimeout are properties on the global object
T or F
T