Object-oriented Programming in JS Flashcards

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
Q

6 element of

Object.defineProperty()

A
Get
Set
Writable
Configurable
Enumerable
Value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

To get the attributes of a property in an object use the method ______ ?

A

Object.getOwnPropertyDescriptor()

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

What uses a return statement,

GET or SET?

A

GET

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

Make an object property read only

A

writable: false

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

// To get the own/instance properties:

A

Object.keys(obj);

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

// To get all the properties (own + prototype):

A

for (let key in obj) {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q
const x = {};
const y = {};
Object.getPrototypeOf(x) === Object.getPrototypeOf(y); //
A

returns true

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

Object.prototype === Object.getPrototypeOf({})

A

TRUE

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

Array.prototype === Object.getPrototypeOf([])

A

TRUE

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

proper way to get a prototype of an object is buy using )))

A

Object.getPrototypeOf( )

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

the object method ____ is the same as __proto___

A

Object.getPrototypeOf()

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

object.prototype is equal too _____

A

obj.__proto__

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

The 2 types of members of objects

A
  1. instance members

2. prototype members

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

What will return (instance + prototype) in a objects:

for(let key in obj) console.log(key);

object.keys(obj)

A

for in loop returns (instance + prototype)

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

______ expresses a lack of identification, indicating that a variable points to no object.

A

Null

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

The only way to create a new JavaScript Date object is to use the _____ operator:

A

new

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

make a new date

A

let x = new date ();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q
const seconds = (endTime.getTime() -startTime.getTime());
returned 13160 

FIX code above to get 13.160

A

(endTime.getTime() -startTime.getTime() / 1000);

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

every object has a constructor property that

A

returns the function that was used to construct the object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q
new Circle.prototype.constructor(1)
// this can be written with a shorter way
A

new Circle(1)

45
Q
// this can be written with a longer way
new Circle(1)
A

new Circle.prototype.constructor(1)

46
Q

When you reset the prototype of an object you should also ________

A

reset the constructor

47
Q
Circle.\_\_\_\_\_\_\_\_\_\_\_ = Circle;
Circle.prototype = Object.create(Shape.prototype);

RESET CIRCLE CONSTRUCTOR

A

prototype.constructor

48
Q

The________ keyword is used in class declarations or class expressions to create a class which is a child of another class

A

extends

49
Q

The________ keyword is used to access and call functions on an object’s parent.

A

super

50
Q

object.assign()

A

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
Q

________ 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.

A

The Object.assign()

52
Q

// Call the super constructor named “Shape”

function Rectangle(color) {
// insert code here    
}
A

Shape.call(this, color);

53
Q

3 names for the parent class:

A

parent
super
base

54
Q

3 names for the child class:

A

Child
derived
sub

55
Q

proper way to getting a prototype of an object is by _____

A

Object.prototypeOf()

56
Q
function Circle(radius){
  this.radius = radius;
}
const newCircle = new Circle(1);
console.log(Object.getPrototypeOf(newCircle));

> constructor: ____
__proto__:______

A

f Circle(radius)

Object

57
Q

let obj = { }:

Object.prototype
is equal too _______

A

obj.__proto__

58
Q

let obj = { }:

obj.__proto__
is equal too _______

A

Object.prototype

59
Q

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);
A
  • Circle.prototype = Object.create(Shape.prototype);

the other is the default

60
Q

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?

A
  • Circle.prototype = Object.create(Object.prototype);
61
Q

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

A

Circle.prototype = Object.create(Shape.prototype);

62
Q

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

A

Circle.prototype.constructor = Circle;

63
Q
const canEat = {
  eat: function(){
    this.hunger--;
    console.log('eating');
  }
}

USE OBJECT.ASSIGN to make a new object and assign canEat

A

Object.assign({},canEat);

64
Q

const p = new Rectangle(); // –> returns

class Rectangle {}

A

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
Q

Class declarations are or are not hoisted

A

NOT hoisted to the top.

66
Q
class Circle {
  constructor (radius){
    this.radius = radius;
  }

draw() {
}
}

//Draw is an INSTANCE METHOD OR STATIC METHOD

A

INSTANCE METHOD

its available on an instance of an class which is an object

67
Q

INSTANCE METHOD

A

its available on an instance of an class which is an object

68
Q

Static methods are _____

A

available on the class itself

69
Q
const Circle = function (){
  this.draw = function() {
    console.log(this);
  }
};
const c = new Circle();
console.log(c); // returns?
A

Circle{draw: ƒ}

70
Q
const Circle = function (){
  this.draw = function() {
    console.log(this);
  }
};
const c = new Circle();
console.log(Circle); // returns?
A
ƒ (){
  this.draw = function() {
    console.log(this);
  }
}
71
Q
const Circle = function (){
  this.draw = function() {
    console.log(this);
  }
};
const c = new Circle();
c.draw(); // returns
A

Circle{draw: ƒ}

72
Q
const Circle = function (){
  this.draw = function() {
    console.log(this);
  }
};
const c = new Circle();
c.draw();

// c.draw(); this is called _____?

A

Method call

73
Q
const Circle = function (){
  this.draw = function() {
    console.log(this);
  }
};
const c = new Circle();
const draw = c.draw;

draw(); // returns?

A

window object

74
Q
const Circle = function (){
  this.draw = function() {
    console.log(this);
  }
};
const c = new Circle();
const draw = c.draw;

draw(); // this is called ?

A

function call

75
Q

How to enable strict mode

A

at top of program write ‘use strict’;

76
Q

‘use strict’;

const Circle = function (){
  this.draw = function() {
    console.log(this);
  }
};
const c = new Circle();
const draw = c.draw;

draw(); // returns and why

A

undefined

in strict mode JS engine more sensitive

77
Q
class Circle {
  draw() {
    console.log(this);
  }
}
let c = new Circle;
c.draw(); //
A

Circle{}

78
Q
class Circle {
  draw() {
    console.log(this);
  }
}
let c = new Circle;
const draw = c.draw;
draw();
A

undefined

by default classes are in strict mode

79
Q

Symbol() === Symbol()

True / False

A

false

every time you call the function you get a unique identifier

80
Q
class Circle {
  constructor(raduis) {
   3 ways to set this to radius
  }
}
A

this.raduis = raduis;
this[‘radius’] = radius;
this[_radius] = raduis;

81
Q

Use ______ to implement private properties and methods in Classes

A

symbols

82
Q

Static method doesn’t use _______

A

this to access properties

83
Q

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.

A

WeakMap

84
Q

______ of WeakMaps are of the type Object only. Primitive data types as keys are not allowed

A

Keys

85
Q

Keys of WeakMaps are of the type Object only. _______ data types as keys are not allowed

A

Primitive

86
Q

splitting code into different files is called ______

A

module

87
Q

to implement a private property on a class use the method ____

A

weakMap()

88
Q

ES6 modules:

the two keywords for ES6 that will allow multiple files

A

import

export

89
Q

what is a transliper

A

combo of translator and compiler

90
Q
function Circle(radius){
    this.radius = radius
    this.draw = function(){
        console.log('draw');
    }
};
const c = new Circle(1);
WRITE ABOVE WITH CALL METHOD
A

Circle.call( { } , 1 );

The new object is being created and the 1 is the argument for the radius

91
Q

what are the 3 reference types?

A

Object
Array
Function

92
Q

what are the 6 value types?

A
number
string
boolean
undefined
null
symbol
93
Q
let x = 10;
let y = x
x = 20
x ?
y?
why?
A

10
20
x and y value types and are independent

94
Q

2 reasons to use Bracket Notation.

circle.location = {x:1};
circle['location'] = {x:1};
A
  1. dynamically access property name
  2. using property names that are not valid identifiers.
    example ‘center-location’ cant be accessed via dot notation.`
95
Q

to check existence of property or method in object use the

A

In operator

96
Q

what will show prototype properties in an object?
For…in loop
Object.keys() method

A

For…in loop

97
Q

best OOP practice in JS, when you reset the prototype of an object you need to also reset the _______

A

constructor

98
Q

in class syntax the methods are stored_____

class Circle {
    constructor(radius){
      // here    
    }
      // or here
}

}

A

the body of the class, not in the constructor

99
Q

The “TYPEOF” the circle class is a _____?

function Circle(radius){
    this.radius = radius;
    this.draw = function(){
        console.log('draw');
    }
}
A

function

100
Q

Function ______ are not hoisted

A

expressions

101
Q

Is this hoisted?

class CircleC {
    constructor(radius){
        this.radius = radius;
    }
    draw(){
        console.log('draw');
    }     
}
A

No,

Class expressions and declarations are not hoisted

102
Q

What is an instance method of CircleC?

class CircleC {
    constructor(radius){
        this.radius = radius;
    }
    draw(){
    }     
}
A

the draw() method as it’s available on an instance of a class which is a object

103
Q

What is a method call?

const Circle = function(){
    this.draw = function(){console.log(this)}
};

const c = new Circle();
draw(); // ?
c.draw(); // ?

A

c.draw()

104
Q

What is a function call?

const Circle = function(){
    this.draw = function(){console.log(this)}
};

const c = new Circle();
draw(); // ?
c.draw(); // ?

A

draw()

105
Q
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 ___

A

the window object

106
Q
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 ___

A

the object

107
Q

what changes the behavior of “this”

A

strict mode

‘use strict’

108
Q

with class inheritance we use the ______ keyword

A

extend

109
Q
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');
    }
}
A

super