Object-oriented Programming in JS Flashcards

(109 cards)

1
Q

What are the 4 pillars of OOP

A

Encapsulation
Abstraction
Polymorphism
Inheritance

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

what is Encapsulation?

A

group related variable and functions together and reuse

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

What is Abstraction?

A

hide details and complexity and only show essentials

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

What is Inheritance?

A

eliminate redundant code

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

What is Polymorphism?

A

allows code to change

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

what is a function in an object called?

A

method.

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

the constructor property that references ______

A

the function that was used to create the object

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

let x = 10

function increase(x){
    x++;
}
increase(x);
console.log(x);//
A

10

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

let obj = {value: 10}

function increase(obj){
    obj.value++;
}
increase(obj);
console.log(obj.value);//
A

11

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

The method to get the keys of an object

A

Object.keys(object);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
function Circle (radius, color){
    this.radius = radius
    // this.color = color;
}

HIDE COLOR FROM THE OUTSIDE

A

// let color = color;

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

What is temporal: SCOPE or CLOSURE?

A

scope

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

A getter is a ___?

A

a function that is used to read a property

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

Make the following falsy

if (value.x || value.y){
// code
}
A

(!value.x || !value.y){

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

Another simple explanation for prototype explanation. A prototype is a __________

A

parent

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

Every object (except the root object) has a prototype (parent). To get the prototype of an object: //

A

Object.getPrototypeOf(obj);

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

Every object in Javascript has a prototype or parent except the _________

A

root object.

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

Objects created by a given constructor will have the same _______

A

prototype

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

________ created by a given constructor will have the same prototype

A

Objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
let person = {name: "steve'};
get prototype or person
A

object.getPrototypeOf(person);

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

to make an element of an object read only:

A

writeable: FALSE

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

The static method ____________ defines a new property directly on an object, or modifies an existing property on an object, and returns the object.

A

Object.defineProperty()

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

to make an element of an object not show up in object.key :

A

enumerable: FALSE

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

to ensure you can not delete a property of an object:

A

configureable: FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
6 element of | Object.defineProperty()
``` Get Set Writable Configurable Enumerable Value ```
26
To get the attributes of a property in an object use the method ______ ?
Object.getOwnPropertyDescriptor()
27
What uses a return statement, | GET or SET?
GET
28
Make an object property read only
writable: false
29
// To get the own/instance properties:
Object.keys(obj);
30
// To get all the properties (own + prototype):
for (let key in obj) {}
31
``` const x = {}; const y = {}; Object.getPrototypeOf(x) === Object.getPrototypeOf(y); // ```
returns true
32
Object.prototype === Object.getPrototypeOf({})
TRUE
33
Array.prototype === Object.getPrototypeOf([])
TRUE
34
proper way to get a prototype of an object is buy using )))
Object.getPrototypeOf( )
35
the object method ____ is the same as __proto___
Object.getPrototypeOf()
36
object.prototype is equal too _____
obj.__proto__
37
The 2 types of members of objects
1. instance members | 2. prototype members
38
What will return (instance + prototype) in a objects: for(let key in obj) console.log(key); object.keys(obj)
for in loop returns (instance + prototype)
39
______ expresses a lack of identification, indicating that a variable points to no object.
Null
40
The only way to create a new JavaScript Date object is to use the _____ operator:
new
41
make a new date
let x = new date ();
42
``` const seconds = (endTime.getTime() -startTime.getTime()); returned 13160 ``` FIX code above to get 13.160
(endTime.getTime() -startTime.getTime() / 1000);
43
every object has a constructor property that
returns the function that was used to construct the object
44
``` new Circle.prototype.constructor(1) // this can be written with a shorter way ```
new Circle(1)
45
``` // this can be written with a longer way new Circle(1) ```
new Circle.prototype.constructor(1)
46
When you reset the prototype of an object you should also ________
reset the constructor
47
``` Circle.___________ = Circle; Circle.prototype = Object.create(Shape.prototype); ``` RESET CIRCLE CONSTRUCTOR
prototype.constructor
48
The________ keyword is used in class declarations or class expressions to create a class which is a child of another class
extends
49
The________ keyword is used to access and call functions on an object's parent.
super
50
object.assign()
The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.
51
________ method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.
The Object.assign()
52
// Call the super constructor named "Shape" ``` function Rectangle(color) { // insert code here } ```
Shape.call(this, color);
53
3 names for the parent class:
parent super base
54
3 names for the child class:
Child derived sub
55
proper way to getting a prototype of an object is by _____
Object.prototypeOf()
56
``` function Circle(radius){ this.radius = radius; } ``` ``` const newCircle = new Circle(1); console.log(Object.getPrototypeOf(newCircle)); ``` >constructor: ____ >__proto__:______
f Circle(radius) Object
57
let obj = { }: Object.prototype is equal too _______
obj.__proto__
58
let obj = { }: obj.__proto__ is equal too _______
Object.prototype
59
Shape.prototype.duplicate = function(){ console.log('duplicate'); } Circle.prototype.draw = function(){ console.log('draw'); } WHAT POINTS CIRCLE BASE TO SHAPE BASE? - Circle.prototype = Object.create(Object.prototype); - Circle.prototype = Object.create(Shape.prototype);
- Circle.prototype = Object.create(Shape.prototype); the other is the default
60
Shape.prototype.duplicate = function(){ console.log('duplicate'); } Circle.prototype.draw = function(){ console.log('draw'); } - Circle.prototype = Object.create(Shape.prototype); PRIOR TO CHANGE WHAT DID THE Circle.prototype point to?
- Circle.prototype = Object.create(Object.prototype);
61
function Shape(){ } ``` function Circle(radius){ this.radius = radius; } ``` Shape.prototype.duplicate = function(){ console.log('duplicate'); } Circle.prototype.draw = function(){ console.log('draw'); } +++++++++++++++++++++++++++++ POINT circle.prototype to shape using Object.create
Circle.prototype = Object.create(Shape.prototype);
62
function Shape(){ } ``` function Circle(radius){ this.radius = radius; } ``` Shape.prototype.duplicate = function(){ console.log('duplicate'); } Circle.prototype.draw = function(){ console.log('draw'); } +++++++++++++++++++++++++++++ POINT circle.prototype.constructor to shape
Circle.prototype.constructor = Circle;
63
``` const canEat = { eat: function(){ this.hunger--; console.log('eating'); } } ``` USE OBJECT.ASSIGN to make a new object and assign canEat
Object.assign({},canEat);
64
const p = new Rectangle(); // --> returns class Rectangle {}
ReferenceError An important difference between function declarations and class declarations is that function declarations are hoisted and class declarations are not. You first need to declare your class and then access it, otherwise code like the following will throw a ReferenceError:
65
Class declarations are or are not hoisted
NOT hoisted to the top.
66
``` class Circle { constructor (radius){ this.radius = radius; } ``` draw() { } } //Draw is an INSTANCE METHOD OR STATIC METHOD
INSTANCE METHOD its available on an instance of an class which is an object
67
INSTANCE METHOD
its available on an instance of an class which is an object
68
Static methods are _____
available on the class itself
69
``` const Circle = function (){ this.draw = function() { console.log(this); } }; ``` ``` const c = new Circle(); console.log(c); // returns? ```
Circle {draw: ƒ}
70
``` const Circle = function (){ this.draw = function() { console.log(this); } }; ``` ``` const c = new Circle(); console.log(Circle); // returns? ```
``` ƒ (){ this.draw = function() { console.log(this); } } ```
71
``` const Circle = function (){ this.draw = function() { console.log(this); } }; ``` ``` const c = new Circle(); c.draw(); // returns ```
Circle {draw: ƒ}
72
``` const Circle = function (){ this.draw = function() { console.log(this); } }; ``` ``` const c = new Circle(); c.draw(); ``` // c.draw(); this is called _____?
Method call
73
``` const Circle = function (){ this.draw = function() { console.log(this); } }; ``` ``` const c = new Circle(); const draw = c.draw; ``` draw(); // returns?
window object
74
``` const Circle = function (){ this.draw = function() { console.log(this); } }; ``` ``` const c = new Circle(); const draw = c.draw; ``` draw(); // this is called ?
function call
75
How to enable strict mode
at top of program write 'use strict';
76
'use strict'; ``` const Circle = function (){ this.draw = function() { console.log(this); } }; ``` ``` const c = new Circle(); const draw = c.draw; ``` draw(); // returns and why
undefined in strict mode JS engine more sensitive
77
``` class Circle { draw() { console.log(this); } } let c = new Circle; c.draw(); // ```
Circle {}
78
``` class Circle { draw() { console.log(this); } } let c = new Circle; const draw = c.draw; draw(); ```
undefined by default classes are in strict mode
79
Symbol() === Symbol() | True / False
false | every time you call the function you get a unique identifier
80
``` class Circle { constructor(raduis) { 3 ways to set this to radius } } ```
this.raduis = raduis; this['radius'] = radius; this[_radius] = raduis;
81
Use ______ to implement private properties and methods in Classes
symbols
82
Static method doesn't use _______
this to access properties
83
The _______ object is a collection of key/value pairs in which the keys are weakly referenced. The keys must be objects and the values can be arbitrary values.
WeakMap
84
______ of WeakMaps are of the type Object only. Primitive data types as keys are not allowed
Keys
85
Keys of WeakMaps are of the type Object only. _______ data types as keys are not allowed
Primitive
86
splitting code into different files is called ______
module
87
to implement a private property on a class use the method ____
weakMap()
88
ES6 modules: | the two keywords for ES6 that will allow multiple files
import | export
89
what is a transliper
combo of translator and compiler
90
``` function Circle(radius){ this.radius = radius this.draw = function(){ console.log('draw'); } }; ``` ``` const c = new Circle(1); WRITE ABOVE WITH CALL METHOD ```
Circle.call( { } , 1 ); The new object is being created and the 1 is the argument for the radius
91
what are the 3 reference types?
Object Array Function
92
what are the 6 value types?
``` number string boolean undefined null symbol ```
93
``` let x = 10; let y = x x = 20 x ? y? why? ```
10 20 x and y value types and are independent
94
2 reasons to use Bracket Notation. ``` circle.location = {x:1}; circle['location'] = {x:1}; ```
1. dynamically access property name 2. using property names that are not valid identifiers. example 'center-location' cant be accessed via dot notation.`
95
to check existence of property or method in object use the
In operator
96
what will show prototype properties in an object? For...in loop Object.keys() method
For...in loop
97
best OOP practice in JS, when you reset the prototype of an object you need to also reset the _______
constructor
98
in class syntax the methods are stored_____ ``` class Circle { constructor(radius){ // here } // or here } ``` }
the body of the class, not in the constructor
99
The "TYPEOF" the circle class is a _____? ``` function Circle(radius){ this.radius = radius; this.draw = function(){ console.log('draw'); } } ```
function
100
Function ______ are not hoisted
expressions
101
Is this hoisted? ``` class CircleC { constructor(radius){ this.radius = radius; } draw(){ console.log('draw'); } } ```
No, Class expressions and declarations are not hoisted
102
What is an instance method of CircleC? ``` class CircleC { constructor(radius){ this.radius = radius; } draw(){ } } ```
the draw() method as it's available on an instance of a class which is a object
103
What is a method call? ``` const Circle = function(){ this.draw = function(){console.log(this)} }; ``` const c = new Circle(); draw(); // ? c.draw(); // ?
c.draw()
104
What is a function call? ``` const Circle = function(){ this.draw = function(){console.log(this)} }; ``` const c = new Circle(); draw(); // ? c.draw(); // ?
draw()
105
``` const Circle = function(){ this.draw = function(){console.log(this)} }; ``` const c = new Circle(); ++++++++++++++++ draw(); when you call a function (function call) call "this" in an object will point to ___
the window object
106
``` const Circle = function(){ this.draw = function(){console.log(this)} }; ``` const c = new Circle(); ++++++++++++++++ c.draw(); when you call a method call "this" in an object will point to ___
the object
107
what changes the behavior of "this"
strict mode | 'use strict'
108
with class inheritance we use the ______ keyword
extend
109
``` class Shape { constructor(color){ this.color = color; } move(){ console.log('move'); } } ``` ``` class Circle(){ constructor(){ //this add error what keyword to fix? } draw(){ console.log('draw'); } } ```
super