Classes Flashcards

1
Q

function Rectangle(length, width) {
this.length = length;
this.width = width;
}

Rectangle.prototype.getArea = function() {
return this.length * this.width;
};

let rec = new Rectangle(10, 5);

console.log(rec.constructor == Rectangle);

A

true
There is a property

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

Create a constructor with the class keyword

A

class Rectangle {
constructor(length, width) {
this.length = length;
this.width = width;
}

getArea() {
return this.length * this.width;
}
}

let rec = new Rectangle(10, 5);
console.log(typeof Rectangle); // function
console.log(rec instanceof Rectangle); // true
console.log(rec.constructor); // [class Rectangle]
console.log(rec.getArea()); // 50

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

What will this log?
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}

area() {
return this.height * this.width
}
}

let apple = new Rectangle(2, 2);

console.log(apple.constructor);

A

[class Rectangle]
(vs function Rectangle that would have happened with the class keyword)

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

What happens with this
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}

area() {
return this.height * this.width
}
}

let apple = Rectangle(2, 2);

A

Type error

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

Create a class expression (rather than class declaration)

A

let Rectangle = class {
constructor(length, width) {
this.length = length;
this.width = width;
}

getArea() {
return this.length * this.width;
}
};

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

What happens with:
function createObject(classDef) {
return new classDef();
}

class Foo {
sayHi() {
console.log(‘Hi!’);
}
}

let obj = createObject(Foo);
obj.sayHi();

A

//=> logs ‘Hi!’

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

Difference between class methods and static methods

A

Same thing,
But in js we call them static methods. Classes are just syntactic sugar

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

Define properties (static properties) and methods an a constructor made using the class keyword

A

class Rectangle {
constructor(length, width) {
this.length = length;
this.width = width;
}

static getDescription() {
return ‘A rectangle is a shape with 4 sides’;
}

getArea() {
return this.length * this.width;
}
}

console.log(Rectangle.getDescription()); // A rectangle is a shape with 4 sides

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

What is logged

class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}

area() {
return this.height * this.width
}
}

Rectangle.poper = 123;
console.log(Rectangle.poper);

A

123

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

Class is synactic sugar, but what are 4 things unique to it?

A
  1. All code in class executes in strict mode.
  2. class declarations are not hoisted
  3. must use new keywordor it throws an error
  4. you can’t reassign the prototype of a class
    ClassName.prototype cannot be reassigned (wlll fail silently)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the typeof of each animal
function Dog() {
}

function Pet(type) {
if (type === ‘dog’) {
return new Dog();
} else if (type === ‘lion’) {
return ‘not a pet!’;
}
}

let dog = new Pet(‘dog’);
let lion = new Pet(‘lion’);
let cat = new Pet(‘cat’);

A

Object
Object
Object

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

Why does this give an error:

const Animal = function(species) {
this.species = species;
return species;
};

Animal.prototype.sleep = function() {
console.log(The ${this.species} is sleeping);
};

let lion = Animal(‘Panthera leo’);
lion.sleep(); // TypeError

A

Animal is called without the new keyword

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

What will this log?

let Applesauce = class {
constructor(height, width) {
this.height = height;
this.width = width;
}

static apple = 123;

area() {
return this.height * this.width;
}
}

let rectangle = new Applesauce(2, 4);

console.log(Applesauce)

A

[class Applesauce] { apple: 123 }

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

When creating objects and setting prototypes with
let newObject = Object.create(object);

Set some beginning properties of newObject using a function on object.

A

let carPrototype = {
start: function() {
this.started = true;
},

stop: function() {
this.started = false;
},

init(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
},
};

let car1 = Object.create(carPrototype);
car1.init(‘Toyota’, ‘Corolla’, 2016);

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

When creating objects and setting prototypes with
let newObject = Object.create(object);

Set some beginning properties of newObject using a function on object. But do it in 1 line.

A

let carPrototype = {
start: function() {
this.started = true;
},

stop: function() {
this.started = false;
},

init(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
return this;
},
};

let car1 = Object.create(carPrototype).init(‘Toyota’, ‘Corolla’, 2016);

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

Give an example of an OLOO function creation system
What does OLOO stand for?

A

Objects Linking to Other Objects

let carPrototype = {
start: function() {
this.started = true;
},

stop: function() {
this.started = false;
},
};

let car1 = Object.create(carPrototype);
car1.make = ‘Toyota’;
car1.model = ‘Corolla’;
car1.year = 2016;

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

Adv and Dis of OLOO vs factory functions (1 each)

A

the OLOO pattern has one significant advantage over factory functions: memory efficiency. Since all objects created with the OLOO pattern inherit methods from a single prototype object, the objects that inherit from that prototype object share the same methods. Factory functions, on the other hand, create copies of all the methods for each new object. That can have a significant performance impact, especially on smaller devices with limited memory.

However, that doesn’t mean that OLOO is decidedly better than the factory pattern. An advantage of the factory pattern is that it lets us create objects with private state. If that doesn’t make sense to you yet, don’t worry. We’ll return to this topic in a later course when we discuss closures.

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

Question 2
https://launchschool.com/lessons/d5964d17/assignments/02f965cb

A

const PetPrototype = {
init(animal, name) {
this.animal = animal;
this.name = name;
return this;
},

sleep: function() {
console.log(“I am sleeping”);
},

wake: function() {
console.log(“I am awake”);
},
};

18
Q

Create a prototypal chain like this, using a Constructor Function (not classes!):
sqr —> Square.prototype —> Rectangle.prototype —> Object.prototype

Make sure the sqr consructor also points to Square

A

function Rectangle(length, width) {
this.length = length;
this.width = width;
}

Rectangle.prototype.getArea = function() {
return this.length * this.width;
};

Rectangle.prototype.toString = function() {
return [Rectangle ${this.length} x ${this.width}];
};

// rect test code omitted

function Square(size) {
Rectangle.call(this, size, size);
}

Square.prototype = Object.create(Rectangle.prototype);
Square.prototype.constructor = Square;

Square.prototype.toString = function() {
return [Square ${this.length} x ${this.width}];
};

// sqr test code omitted

19
Q

Difference between prototypal inheritance and pseudo-classical inheritance

A

Prototypal inheritance is inheritance object to object.

Pseudo classical inheritance is when the prototype is an object on a constructor

20
Q

Problem at the bottom:
https://launchschool.com/lessons/d5964d17/assignments/006358da

A

https://launchschool.com/lessons/d5964d17/assignments/006358da

21
Q

Using the class keyword, have a class prototype reference another

A

class Rectangle {
constructor(length, width) {
this.length = length;
this.width = width;
}

getArea() {
return this.length * this.width;
}

toString() {
return [Rectangle ${this.width * this.length}];
}
}

class Square extends Rectangle {
constructor(size) {
Rectangle.call(this, size, size);
}

toString() {
return [Square] ${this.width * this.length};
}
}

22
Q

Using the super keyword, have a class use the constructor method of the parent class

basically equivalent to this:
Rectangle.call(this, size, size);

A

class Rectangle {
constructor(length, width) {
this.length = length;
this.width = width;
}

getArea() {
return this.length * this.width;
}

toString() {
return [Rectangle ${this.width * this.length}];
}
}

class Square extends Rectangle {
constructor(size) {
super(size, size);
}

toString() {
return [Square] ${this.width * this.length};
}
}

23
Q

Do a Class declaration and then a class expression using the extends keyword

A

class Student extends Person {

let Student = class extends Person {

23
Q

Practice Problems
https://launchschool.com/lessons/d5964d17/assignments/16921628

A

https://launchschool.com/lessons/d5964d17/assignments/16921628

24
Q

What’s a mix-in, what problem does it solve

A

It’s the mix of prototypal inheritance and factory function (the same method on all instances for example).

It solves the problem where it seems like one object should have 2 prototypes.

25
Q

Create a mix-in with birds of subtype flying or swimming birds which are subtypes of bird

Birds can be swiming, flying, or swiming and flying.

A

Basically with Object.assign
https://launchschool.com/lessons/d5964d17/assignments/e7850b07

26
Q

Create factory functions with birds of subtype flying or swimming birds which are subtypes of bird

A

https://launchschool.com/lessons/d5964d17/assignments/e7850b07

const Swimmable = {
swim() {}
}

class Bird {}

class FlyingBird extends Bird {
fly() {}
}

class Stork extends FlyingBird {}

class Parrot extends FlyingBird {}

class Penguin extends Bird {}
Object.assign(Penguin.prototype, Swimmable);

class Ostrich extends Bird {}
Object.assign(Ostrich.prototype, Swimmable);

class Duck extends FlyingBird {}
Object.assign(Duck.prototype, Swimmable);

class Goose extends FlyingBird {}
Object.assign(Goose.prototype, Swimmable);

27
Q

Practice Problems:
https://launchschool.com/lessons/d5964d17/assignments/e7850b07

A

https://launchschool.com/lessons/d5964d17/assignments/e7850b07

28
Q

Return the name of a function
Return the name of a constructor

(for example when the function is declared or a function expression assigned to a variable)

A

theFunction.name
anObject.constructor.name

29
Q

What is polymorphism? What’s the benefit?
What’sthe difference between polymorphism by inheritence and polymorphism by ducktyping.

A

Different objects repond in different ways to the same message.
Data of different types responding to a common interface.

Polymorphism refers to the ability of objects with different types to respond in different ways to the same message (or method invocation); that is, data of different types can respond to a common interface.

It makes the code more manageable.

By inheritence: When a subclass has the same method name as a superclass

By Ducktyping: When different objects have identical method names

30
Q

Describe two different polymorphic implementations

A

You could have a chain of methods (Polymorphism Through Inheritance):
https://launchschool.com/lessons/d5964d17/assignments/22f0ecca#:~:text=to%20implement%20polymorphism.-,Polymorphism,-Through%20Inheritance

Or just objects with the same name of method (Polymorphism Through Duck Typing)
https://launchschool.com/lessons/d5964d17/assignments/22f0ecca#:~:text=Polymorphism%20Through%20Duck%20Typing

31
Q

The Quiz
https://launchschool.com/quizzes/a3cb12b5/edit

A

https://launchschool.com/quizzes/a3cb12b5/edit

32
Q

https://launchschool.com/lessons/e3c64e3f/assignments/cb0a5ff7

A

https://launchschool.com/lessons/e3c64e3f/assignments/cb0a5ff7

33
Q

https://launchschool.com/lessons/1eaf5e37/assignments/39b60e49

A

https://launchschool.com/lessons/1eaf5e37/assignments/39b60e49

34
Q

https://launchschool.com/lessons/1eaf5e37/assignments/69e660e6

A

https://launchschool.com/lessons/1eaf5e37/assignments/69e660e6

35
Q

https://launchschool.com/lessons/fb892747/assignments/271844ae

A

https://launchschool.com/lessons/fb892747/assignments/271844ae

36
Q

The class that inherits properties from an object is called the…

The class this object inherits from is called the

A

Subclass

SuperClass

37
Q

What does this do

class AClass {
afunction = function() {
console.log(‘applesauce’);
}
}

let anObject = new AClasss()
console.log(anObject)

A

AClass { afunction: [Function: afunction] }

the prototype methods need to be in the format of
afunction() {
}

38
Q

What does this log to console?
function Rectangle(length, width) {
this.length = length;
this.width = width;
}

Rectangle.prototype.getArea = function() {
return this.length * this.width;
};

Rectangle.prototype.toString = function() {
return Rectangle ${this.length} x ${this.width};
};

function Square(size) {
Rectangle.call(this, size, size);
}

Square.prototype = Object.create(Rectangle.prototype);

Square.prototype.toString = function() {
return Square ${this.length} x ${this.width};
};

let square = new Square(2,5);

A

[Function: Rectangle]

39
Q

Where is an object’s constructor property?

A

On the prototype

object.constructor
Is really like going:
Object.getPrototypeOf(object).constructor

40
Q

When does instanceof return true?

A
  • When the object was created using the new keyword
  • the object after ‘instanceof’ is in the prototype chain for the object before
41
Q

class Critter {}
class Snake extends Critter {}
class Rattler extends Snake {}

rattler is a subtype of critter
T or F

A

F

42
Q

What happens if you omit the constructor function from a class that extends another

A

It basically uses the parent class’ constructor as if you had a contructor function and super() was called.