OOP_Javascript Flashcards

1
Q

Why is this course important?

(Hint: Immense career impact)

A

Why?

A lot of developers write JavaScript applications without properly understanding the inner workings of JavaScript.

Don’t be one of these!

Get a solid foundation of the inner workings of JavaScript

It’s an immense help to your career

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

What are technical interviewers looking for?

(Hint: Object Oriented Programming)

A

Understanding of Object Oriented Programming

Why?

makes a candidate stand out!

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

What are popular interview questions?

A

Pillars of Object Oriented Programming:

Encapsulation

Abstraction

Inheritance

Polymorphism

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

What does this course teach?

A

OOP principles + how to implement in Javascript!

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

What is procedural programming?

A

A style of programming

bunch of variables that contain data

a number of functions that act on the data

As program grows, you have functions all over the place

You will copy and paste functions all over the place

If you change one function the others break

Soln?

Object Oriented Programming

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

What are the benefits of Object Oriented Programming?

A
  1. Reduce complexity, increase reusability - group related variables and functions together to reduce complexity + increase reusability in applications and programs (encapsulation)
  2. Hide complexity - hide complexities from consumers, reducing complexity of applications, increasing speed and safety of programs (abstraction)
  3. Reduce redundant code - reduce redundant code by allowing objects to inherit code from a parent (Inheritance)
  4. Eliminate switch cases - eliminate switch cases by allowing objects to override inherited methods (Polymorphism)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is object-oriented programming?

A

a very popular style of programming

It’s a model or pattern of programming

centered around objects rather than functions

OOP is not a programming language or tool!

It’s a style or model of programming

many frameworks are built using object oriented programming

HLL support OOP -

C#, Java, Python and Javascript

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

What problem does object oriented programming solve?

A

Spagetti code -

procedures (functions) all interconnected

change one function, other’s break

too much interconnection between functions

Soln?

combine functions and data into units (objects)

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

What problem does encapsulation solve?

(Hint: too much interconnectedness)

A

Spagetti code

​Hint: too much interconnectedness

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

What problem does abstraction solve?

A

This helps us reduce impact of change

simpler interface for objects

Making changes to the private method doesn’t cause issues in rest of the applications code because these changes don’t leak to the outside

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

What problem does inheritance solve?

(Hint: reduce redundant code)

A

It helps us eliminate redundant code

We can define properties and methods once in a generic object

other objects inherit these properties and methods

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

What are the two types of inheritance?

A

Classical vs. prototypical

Javascript doesn’t have classes and inheritance is implemented using prototypical inheritance!

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

What problem does polymorphism solve?

(Hint: elminate if/else and switch/case statements)

A

allows us to get rid of switch/case statements

Example:

We can implement a render method in each object

render method will behave differently depending on type of object referencing

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

What are the topics covered in objects?

A

Javascript is all about objects:

Creating objects

factoring constructors

adding, removing properties

private properties

getters/setters

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

How do we define an object?

A

use let or const

Why?

ES6 scope

don’t use var (scope issues)

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

What is object literal syntax?

(Hint: avoid for objects with methods)

A

Don’t use with objects that have behavior

Why?

If changes to a function is required, have to update multiple spots of code

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

What is an object in Javascript?

A

collection of key-value pairs

we can have key and value as a function

we can have a key and value as an object

This object has three “members”

If a member is a function we refer to it as a “method.”

the other members are “properties”

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

What is an object property?

(Hint: object member)

A

A member of an object that holds a value is called a property

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

What is an object method?

A

A function defined inside an object

called a method and is used to define some logic

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

How can we access an object’s members?

(Hint: dot or bracket)

A

access values and methods

using the dot notation

Bracket notation

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

In what ways can we define objects?

A

using object literals (simple) curly braces

using factories, using constructors

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

When do we say an object has “behavior”?

A

If an object has more than one method we say that object has “behavior”

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

Why is object literal syntax not a good way to create and duplicate an object with behavior?

(Hint: copy code to duplicate)

A

You will have to copy the exact code to duplicate the object

If there is a bug in the function, you’ll have to change in multiple places

It acts like procedural programming

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

When is object literal syntax not a problem?

A

if the object doesn’t have behavior or methods

Only has variables and values

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

What is the solution to the object literal syntax issue?

A

Use a factory or constructor function.

Java and C++ developers prefer constructor functions

Why?

Their syntax using the new keyword looks similar to Java

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

What is a factory function?

A

Used for creating objects

better than object literal syntax

Why?

reduce redundent code

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

What is the naming convention for constructor functions?

(Hint: Pascal)

A

Pascal case

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

What is the “.this” operator?

A

references or points to the global object “class” stored in memory

updates the values for that object.

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

When we use the new operator to call a function, what three things happen?

A
  1. New operator creates an empty object
  2. Sets “this” to point to that object
  3. Returns the object from this statement
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

When do we refer to a function as a constructor function?

A

When we use the .this keyword along with the new operator

we refer to the function as a constructor function.

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

When do we refer to a function as a factory function?

A

When we return an object from a function we refer to it as a factor function

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

Why do C# developers prefer constructor function syntax?

A

It looks like creating an instance of a class

BUT in Javascript we don’t really have classes.

33
Q

Which pattern in Javascript is best for creating an object?

(Hint: Factory vs. Constructor)

A

Neither! They both work.

Be familiar with both ways.

  1. Factory
  2. Constructor
34
Q

What is a constructor property?

A

Every object has a constructor property that references function used to create object

Ex.

When we build an object it has a constructor property which is a function that is used to make the object.

Object literal syntax - internally JS engine uses Object constructor function

Factory Function - internally JS engine uses Object constructor function

Constructor Function - uses constructor function defined to create object

35
Q

What happens when we declare an object using object literal syntax?

(Hint: Constructor function)

A

JS engine internally uses the object constructor function

36
Q

What are functions in Javascript?

A

in Javascript functions are objects!

The object (function) has properties and methods!

37
Q

How are value type and reference type variables different?

A

Absolutely important for understanding prototypes

value types copy data directly into new variables (making them completely independent)

reference types are copied by their reference (making them point to same value)

38
Q

How are reference types different than value types?

(Hint: copied by reference vs. independent copy of value)

A

Passing a value type vs. reference type to a function:

value type - arg value is copied independently into local variable, both point to separate variables

reference type - obj reference copied into local variable, both point to same object

39
Q

What is the classical inheritance definition?

A

solves redundent code

allows us to reduce the impact of change

allows us to reduce redundent code

40
Q

What don’t we have in Javascript?

(Hint: classes)

A

We only have objects

prototypical inheritance vs. classical inheritance

41
Q

What are the two types of inheritance?

(Hint: classical vs. prototypical)

A

Interview question:

classical - java, c++, python with classes

prototypical - javascript with no classes

42
Q

What is a prototype?

(Hint: parent)

A

a parent

prototypical inheritance -

parent’s members linked to child object

Ex.

x references BaseObject (prototype)

x object inherts all members

y references BaseObject (prototype)

therefore,

there is a single instance of the BaseObject in memory

43
Q

What is multi-level inheritance?

(Hint: multiple prototypes)

A

When an object has multiple prototypes (parents)

myArray []

inherits from [] prototype

also inherits from Object prototype

44
Q

What are property descriptors?

(Hint: Prototype members have attributes)

A

New Objects inherit (point to) Base Object Prototype

Cannot iterate all properties in Base Object Prototype

Why?

Property Descriptors

Ex. .toString ( ) descriptors

configurable (true) : can delete member

enumerable (false) : cannot enumerate

writable (true) : can override implementation

45
Q

How can we define a property descriptor?

(Hint: give a member property attributes)

A

By Default:

configurable (true) : can delete member

enumerable (false) : can enumerate

writable (true) : can override implementation

46
Q

Do constructor functions have prototypes?

A

Yes,

Functions are objects

Constructor Prototype is baseObject prototype

Ex. Array Constructor under hood, references Array Base Prototype, references Object Base Prototype

47
Q

What’s the problem with this implementation?

(Hint: memory allocation)

A

.draw ( ) is an instance member

a new copy of the draw method is instantiated everytime we create a new circle object

that wastes a lot of memory!

In real world, would have multiple instance methods for thousands of objects copied!

Soln?

Prototype members

Add draw ( ) method to prototype (single instance in memory)

48
Q

What two kinds of members do we have in Javascript?

(Hint: instance vs. prototypical)

A

Instance members - accessed via object, unique instance per object in memory

Prototypical members - accessed via prototype, single instance in memory

Can access instance members in prototype function

Can access prototype members in instance function

49
Q

How do we iterate instance vs. prototype members?

(Hint: Object.keys, for in loop)

A
50
Q

What should we avoid doing in Javascript?

(Hint: modifying the built in Objects)

A

Javascript is a dynamic language

But…

Don’t modify objects you don’t own

Avoid modifying built in objects in Javascript

don’t remove existing properties/methods

don’t override methods

don’t add new methods/properties

Why?

If using a third party library, chances are someone already extended these objects and your changes will cause issues

future Javascript versions may have updates to these objects

51
Q

What shouldn’t we do?

(Hint: optimize without consideration)

A

adding instance members to an object’s prototype is an optimization technique

Do not do it if the object doesn’t require multiple instances in memory

Ex. Stopwatch

only requires one instance in memory

moving instance members to prototype caused issues

putting methods in prototypes was bad idea

we knew we wouldn’t have thousands of stopwatch objects in memory

we optimized without considerating we wouldn’t have many instances in memory and caused issues with abstraction principle

52
Q

How can we write code to have an object inherit from a given prototype?

(Hint: prototypical inheritance)

A

Only inherits prototypical members (not instance members)

53
Q

What is a best practice for resetting the prototype of an object (prototypical inheritance)?

(Hint: reset the constructor)

A

Reset the constructor

54
Q

How do we call the super constructor?

(Hint: parent’s constructor)

A

.call method

55
Q

How can we write code that allows us to implement prototypical inheritance?

(Hint: encapsulate in a method)

A
56
Q

How do we override a method in a prototype?

A

How?

JS engine walks up prototypical chain and picks first implementation

duplicate method on child object is used

57
Q

How do we implement polymorphism in Javascript?

A

Very powerful concept in object oriented programming

58
Q

What are some of the problems with inheritance?

A

Don’t overuse this

Avoid creating inheritance hierarchies

Don’t go more than one level

Keep it simple

Favor composition over inheritance

Soln?

Mixins = composition in Javascript

59
Q

How can we write code to accomplish composition in Javascript?

(Hint: Mixins)

A

Object.assign ( )

60
Q

How can we write code to inherit from an instance member vs. prototypical member?

(Hint: Child.prototype = new Parent ( ) )

A
61
Q

What do we have in ES6 (Javascript 2015)?

(Hint: Syntactical sugar over prototypical inheritance)

A

Classes

Not classes like Java

Rather,

Syntactical sugar over prototypical inheritance

Why?

It’s cleaner and simpler

Implementing inheritance is simplier

62
Q

What is the critical difference between function expressions and function declarations?

(Hint: Hoisting)

A
63
Q

What is the difference between static and instance methods?

A

Instance methods are available on the object (specific to object)

Static methods are available on the class itself (utility methods)

64
Q

When using function calls, what does the .this keyword point to?

(Hint: draw() vs. c.draw() )

A

Global object

Why?

new operator creates new obj, points .this to it

function call w/o new operator points to global obj

65
Q

What does ‘use strict’ do to the ‘this’ keyword?

(Hint: restricts this from pointing to global object)

A

restricts .this from pointing to global object

Why?

it’s bad practice to modify the global object

66
Q

By default, how are the bodies of our ES6 classes executed?

(Hint: this keyword?)

A

in ‘use strict’

prevents this keyword from pointing to global object

67
Q

What are the approaches for implementing private methods in our ES6 classes?

(Hint: abstract in ES6 using symbols, weakmaps)

A

Symbols

weakmaps

68
Q

What are WeakMaps?

(Hint: HashMaps that store our private fields)

A

hashmaps

(Key = Object, Value = anything)

we can use to store ‘private’ members

Why?

takes them off an object instance

69
Q

How do we implement getters/setters using ES6 classes?

A
70
Q

How do we implement inheritance in ES6?

(Hint: extends keyword)

A

Don’t have to reset constructor

71
Q

How do we override a method using ES6?

(Hint: JS engine walks up tree from Child to parent)

A
72
Q

How do we write code to implement a stack in Javascript?

(Hint: weakMaps for items array)

A
73
Q

What is a module?

(Hint: split code into files, each file is a module)

A

ES6 modules

Splitting code into multiple files

vs.

one long file with source code

Benefits of Modules:

Maintainability

code is better organized, helping us maintain it better

Reuse

can reuse one or more modules in different parts of application or different applications

Abstract

can hide code and only expose essentials

Ex.

hide weakMap only expose a class from a module

74
Q

What are CommonJS modules?

A

Used in Node.js

export

module.exports.Class = ClassName

import

const Class = require (‘./ClassName’)

75
Q

What are ES6 modules?

A

Used in browsers (React)

use export keyword

76
Q

What are ES6 tooling?

(Hint: Transpiler, Bundler)

A

important for browswer applications only

not if using node.js

Transpiler

translater and compiler - give it modern Javascript code, converts to code all browser can understand (ex. Babble)

Bundler -

combines all javascript file into a single file (bundle)

Ex. Webpack -

combines in single file, minify code, uglify code by shortening names of variables, reduces size of code to client

77
Q

How do we install babel?

(Hint: babel converts modern JS to fit all browsers)

A

babel-cli@6.26.0 : command line interface

babel-core@6.26.0 : code to transpose modern javascript into all applicable javascript (browswer) implemented

babel-present-env@1.6.1 : all new features in ES6 is a package, combination of all plugins, supports all functions (arrows, classes, etc)

Ex. compiled down index.js into build folder using babel

real applications, before putting in a bundle, compile each file in babel then use webpack

78
Q

What is the workflow we should use on day to day?

A

Webpack:

sudo npm i -g webpack-cli@2.0.14

webpack-cli init

follow init instructions