Object Creation Patterns Flashcards

1
Q

2 disadvantages of factory functions

A

Each object created by the factory function has a copy of all the methods, which can be redundant and memory intensive.
(basically the benefit of prototypes)

There is no way to tell which factory function created an object, so there’s no way to be sure that you’re working with the right kind of object.
(which is the benefit of constructors)

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

Problems 3 -5
https://launchschool.com/lessons/e3c64e3f/assignments/bf77a962

A

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

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

The object oriented code makes these questions easier to answer (5 things, 3 chunks)

A

How do we create that object?
Where should we add new properties and methods?

What are the properties of any given object?
What operations can I perform on that vehicle?

What are the important concepts in the program?

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

Describe the constructor algorithm (5 things)

A

1 It creates an entirely new object.
2 It sets Constructor.prototype as the prototype for the new object. That is, the new object inherits from the object referenced by Constructor.prototype.
3 It sets the value of this for use inside the function to point to the new object.
4 It invokes the function. Since this refers to the new object, we use it within the function to set the object’s properties and methods.
5 Finally, once the function finishes running, new returns the new object even though we don’t explicitly return anything. If something is returned the object is returned instead. If ANOTHER object is returned, that OTHER object is returned instead

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

What happens if you invoke a constructor function without the new keyword?

A

It executes the function body as normal. But without assigning this to a newly created object, “this” applies to the global object. So it puts a bunch of new properties on the global object.

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

What happens with this:

let apple = () => {
this.apple = “sauce”;
};

let applesauce = new apple();

A

Type Error

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

What happens with this:

let foo = {
Car: function(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
};

let car1 = new foo.Car(‘Toyota’, ‘Camry’, 2019);
car1.make;

A

//=> ‘Toyota’

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

What happens with this?

let foo = {
Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
};

new foo.Car();

A

//=> Uncaught TypeError: foo.Car is not a constructor

concise syntax (also called a concise method) won’t work.
ie. Car(make, model, year) vs Car: function(make, model, year)

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

new doesn’t work with these T or F

new console.log();
new Math();
new parseInt(“3”);
new Date();

A

T
//=> Uncaught TypeError: console.log is not a constructor
for the first 3,
but it works with Date();
//=> 2019-06-26T02:50:20.191Z

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

What happens with

function Cat(name, breed, weight) {
this.name = name;
this.breed = breed;
this.weight = weight;

return ‘a cat’;
}

let fluffy = new Cat(‘fluffy’, ‘Persian’, 15);
fluffy.weight;

A

// 15

The rule here is that if a constructor explicitly tries to return an object, then that object is returned instead of the new object created with the use of new. In all other situations, it returns the newly created object, provided no errors occur. In particular, the constructor ignores explicit return values that are primitives.

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

What happens with

function Cat(name, breed, weight) {
this.name = name;
this.breed = breed;
this.weight = weight;

return { foo: 1 };
}

let fluffy = new Cat(‘fluffy’, ‘Persian’, 15);
fluffy.weight;
fluffy.foo;

A

// undefined
// 1

The rule here is that if a constructor explicitly tries to return an object, then that object is returned instead of the new object created with the use of new. In all other situations, it returns the newly created object, provided no errors occur. In particular, the constructor ignores explicit return values that are primitives.

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

How to avoid this noise:

function Car(make, model, year, color, passengers, convertible, mileage) {
this.make = make;
this.model = model;
this.year = year;
this.color = color;
this.passengers = passengers;
this.convertible = convertible;
this.mileage = mileage;
this.started = false;

this.drive = function() {
this.started = true;
};

// rest of the methods
}

A

One common technique that we can use to manage our parameters better involves passing them to our constructor with an object argument:

let civicArgs = {
make: ‘Honda’,
model: ‘Civic’,
year: 2016,
color: ‘black’,
passengers: 5,
convertible: false,
mileage: 16000
}

let civic = new Car(civicArgs);

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

If we use an object as our constructor argument
How can we avoid this noise?

function Car(args) {
this.make = args.make;
this.model = args.model;
this.year = args.year;
this.color = args.color;
this.passengers = args.passengers;
this.convertible = args.convertible;
this.mileage = args.mileage;
this.started = false;

this.drive = function() {
this.started = true;
};

// rest of methods
}

A

Object.assign

function Car(args) {
Object.assign(this, args);

this.drive = function() {
this.started = true;
};

// rest of the methods
}

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

Issue with the object.assign method inside the constructor function

A

However, one drawback of the Object.assign approach is that the args object may contain properties that the Car object doesn’t need. Those additional properties will, nevertheless, get added to the Car object. Those properties may just be excess baggage for the objects to carry around, but they may also cause trouble.

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

Return boolean if an object was created using a specific constructor function

A

object instanceof Constructor
ex:
civic instanceof Car

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

What is the implicit execution context of a method inside an object created with a contstructor?

A

The new object

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

What happens with:

function Lizard() {
this.scamper = function() {
console.log(“I’m scampering!”);
};
}

let lizzy = Lizard();
lizzy.scamper(); // ?

A

Type error

undefined.undefined is what it’s tryin to do

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

Create a constructor function that defines the prototype to something othe than default

A

let DogPrototype = {
bark() {
console.log(this.weight > 20 ? ‘Woof!’ : ‘Yip!’);
}
};

function Dog(name, breed, weight) {
Object.setPrototypeOf(this, DogPrototype);
this.name = name;
this.breed = breed;
this.weight = weight;
// this.bark method removed.
}

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

Make a constructor function that “defines the prototype”, but use the native prototype setting functionality of constructor functions

A

function Dog(name, breed, weight) {
// deleted Object.setPrototypeOf(this, Dog.myPrototype);
this.name = name;
this.breed = breed;
this.weight = weight;
}

Dog.prototype.bark = function() {
console.log(this.weight > 20 ? ‘Woof!’ : ‘Yip!’);
};

let maxi = new Dog(‘Maxi’, ‘German Shepherd’, 32);
maxi.bark(); // ‘Woof!’

let biggie = new Dog(‘Biggie’, ‘Whippet’, 9);
biggie.bark(); // ‘Yip!’

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

What’s the difference between an object prototype and a function prototype?

A

The object prototype is just a prototype for an object.
Ex.
If bar is an object, then the object from which bar inherits is the object prototype.

The function prototype is the constructor’s prototype object, exists on ConstructorObject.prototype. Any objects created by this constructor have their prototype set to the constructor’s prototype object.
Ex:
if the constructor’s name is Foo, then Foo.prototype references the constructor’s prototype object

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

By default, constructor functions set the object prototype for the objects they create to Object.prototype.
T or F

A

F
By default, constructor functions set the object prototype for the objects they create to the constructor’s prototype object.

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

When does the function’s prototype get used

A

All functions in JavaScript (that are not arrow functions) have a prototype property. However, JavaScript only uses it when you call that function as a constructor, that is, by using the new keyword.

23
Q

What does this do?

function ConstructorOfDog(breed) {
this.breed = breed;

this.apple = function() {
console.log(this);
};
}

let purple = new ConstructorOfDog(‘ads’);

let orange = purple.apple;

orange();

A

Logs the global object

24
Q

Return an objects constructor

A

anObject.constructor
or the objects constructor.prototype
Object.getPrototypeOf(aDog).constructor

25
Q

Is referencing a function’s .prototype.constructor always accurate?

A

no.
the constructor property can be reassigned like any property

26
Q

Only constructor functions have prototypes.Regular functions do not have prototypes.

A

F
All functions in JavaScript (that are not arrow functions) have a prototype property. However, JavaScript only uses it when you call that function as a constructor, that is, by using the new keyword.

27
Q

Rewrite this to work (and make sense):

let RECTANGLE = {
area: function() {
return this.width * this.height;
},
perimeter: function() {
return 2 * (this.width + this.height);
},
};

function Rectangle(width, height) {
this.width = width;
this.height = height;
this.area = RECTANGLE.area();
this.perimeter = RECTANGLE.perimeter();
}

let rect1 = new Rectangle(2, 3);

console.log(rect1.area);
console.log(rect1.perimeter);

A

let RECTANGLE = {
area: function() {
return this.width * this.height;
},
perimeter: function() {
return 2 * (this.width + this.height);
}
};

function Rectangle(width, height) {
this.width = width;
this.height = height;
this.area = RECTANGLE.area.call(this);
this.perimeter = RECTANGLE.perimeter.call(this);
}

let rect1 = new Rectangle(2, 3);
console.log(rect1.area); // => 6
console.log(rect1.perimeter); // => 10

28
Q

What happens with this?

function Ninja() {
this.swung = true;
}

let ninja = new Ninja();

Ninja.prototype.swingSword = function() {
return this.swung;
};

console.log(ninja.swingSword());

A

true

29
Q

What happens with this?

function Ninja() {
this.swung = true;
}

let ninja = new Ninja();

Ninja.prototype = {
swingSword: function() {
return this.swung;
},
};

console.log(ninja.swingSword());

A

Uncaught TypeError: ninja.swingSword is not a function

Despite the similarities to the code in the previous question, this code doesn’t work the same way. That’s because we’re reassigning Ninja.prototype to an entirely new object instead of mutating the original prototype object. The prototype for the ninja object doesn’t change; it’s still the original prototype defined during the constructor’s invocation. Thus, JavaScript can’t find the swingSword method in the prototype chain of ninja.

30
Q

let ninjaA;

{
const Ninja = function() {
this.swung = false;
};

ninjaA = new Ninja();
}

// create a ninjaB object here; don’t change anything else

console.log(ninjaA.constructor === ninjaB.constructor) // => true

A

let ninjaA;

{
const Ninja = function() {
this.swung = false;
};

ninjaA = new Ninja();
}

let ninjaB = new ninjaA.constructor();

console.log(ninjaA.constructor === ninjaB.constructor) // => true

31
Q

Have both the constructor, factory function, and string return in the same function

function User(first, last) {
// …
}

let name = ‘Jane Doe’;
let user1 = new User(‘John’, ‘Doe’);
let user2 = User(‘John’, ‘Doe’);

console.log(name); // => Jane Doe
console.log(user1.name); // => John Doe
console.log(user2.name); // => John Doe

A

function User(first, last){
if (!(this instanceof User)) {
return new User(first, last);
}

this.name = first + ‘ ‘ + last;
}

let name = ‘Jane Doe’;
let user1 = new User(‘John’, ‘Doe’);
let user2 = User(‘John’, ‘Doe’);

console.log(name); // => Jane Doe
console.log(user1.name); // => John Doe
console.log(user2.name); // => John Doe

32
Q

What do you call the object in relation to the constructor or factory that made it?

A

an instance of that constructor or factory

An instance is just another term for the objects created using any means of defining multiple objects of the same kind (e.g., dogs). The term object is more general, while instance is more specific.

33
Q

What are the properties called on an instance

A

instance properties

34
Q

What do we call the methods an instance may use (even if on a prototype)

A

instance methods
also instance properties but ‘methods’ to distinguish

35
Q

What do we call properties or methods on the constructor?

A

Static properties
Status methods

36
Q

What happens with this:
> new Array(3)

A

Creates an array of length 3
[ <3 empty items> ]

37
Q

> new Array(3.1415)

A

=> RangeError: Invalid array length

38
Q

> new Array(-5)

A

=> RangeError: Invalid array length

39
Q

Make an array like this using Array as a constructor
an array with 3 elements that are stars

A

(new Array(3)).fill(‘*’)
Are the parenthesis necessary?

No. But maybe looks more clear.

40
Q

> Array(1, 2, 3)
is the same as
new Array(1, 2, 3)

T or F

A

T
But always use new

41
Q

True or false:

> let numbers = [1, 2, 3]
Object.getPrototypeOf(numbers) === Array.prototype
true

A

T

42
Q

What happens with:
> Array.from({0: ‘a’, 1: ‘b’, 2: ‘c’, length: 3})

A

[‘a’, ‘b’, ‘c’]

43
Q

> let str1 = ‘abc’
typeof str1

> let str2 = new String(‘xyz’)
typeof str2

A

‘string’

‘object’

> ‘abc’ === ‘abc’
true

> new String(‘abc’) === new String(‘abc’)
false

44
Q

> let str = String(‘abc’)
typeof str

A

‘string’
String(‘abc’) and new String(‘abc’) are not the same!
(same with Number and Boolean)

45
Q

Add a method to a built in constructor object (like Array, String, or Object)

A

Array.prototype.applesauce = function() {}

But avoid doing it

46
Q

Call an Array prototype method on a string

A

let string = ‘EEE’;
Array.prototype.every.call(string, char => char === ‘E’); // => true

or

[].every.call(string, char => char === ‘E’); // => true

47
Q

> new String(‘abc’) === new String(‘abc’)
String(‘abc’) === new String(‘abc’)

A

false
false

48
Q
A
49
Q

Have a constructor funtion create an object, but the body basically executes another contructor’s body

A

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

50
Q

Make a constructor function prototype, inherit from another constructor function prototype

A

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

51
Q

Why can’t you just go ThisConstructor.prototype = ThatConstructor.prototype
When you want some prototypal inheritance?

A

You can, but then ThatObject constructor doesn’t really have it’s own prototype.

52
Q

Do all functions have a .prototype property?

A

Yes

53
Q

What’s the difference between static and instance methods

A

Instance methods rely on each object’s specific data, while static methods must NOT rely on data from a specific object.

All objects of a particular type might have behavior that is not dependent at all on member variables; these methods are best made static. By being static, no instance of the class is required to run the method.