Object Oriented Programming Flashcards

1
Q

create a basic object

A
var dog = {
name: "Fido",
numLegs: 4
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

call object’s properties

A

object.property

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

Methods are properties that are

A

functions

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

Constructors are defined with _____ to distinguish them from other functions that are not constructors.

A

a capitalized name

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

Verify an Object’s Constructor with

A

instanceof

instanceof allows you to compare an object to a constructor, returning true or false based on whether or not that object was created with the constructor.

let crow = new Bird(“Alexis”, “black”);

crow instanceof Bird; // => true

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

every instance of Object will have its own copy of these properties T/F?

A

true

collect own properties from each instance in an array like so

let canary = new Bird("Tweety");
let ownProps = [];
// Only change code below this line
for(let property in canary){
  if(canary.hasOwnProperty(property)){
    ownProps.push(property);
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Properties in the prototype are shared…

A

shared among ALL instances of Object

this is a way to store common properties for many instances, if the values of these properties don’t change

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

how to add property to prototype

A

Object.prototype.property = value;

define this outside of the object constructor;

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

Own properties are defined _____. And prototype properties are defined _____

A

directly on the object instance itself

on the prototype

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

the constructor property of an object is ____

A

is a reference to the constructor function that created the instance

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

The advantage of the constructor property is that it’s possible to ____

A

check for this property to find out what kind of object it is

it can help determine if candidate belongs in a group, if their constructor refers to a particular super class/object (Crow > Bird)

it’s generally better to use the instanceof method to check the type of an object.

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

How to add prototype properties at scale, not 1 by 1

A

set the prototype to a new object that already contains the properties

Bird.prototype = {
  numLegs: 2, 
  eat: function() {
    console.log("nom nom nom");
  },
  describe: function() {
    console.log("My name is " + this.name);
  }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

There is one crucial side effect of manually setting the prototype to a new object:

A

It erases the constructor property

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

To fix the issue of erasing the constructor property when manually setting the prototype to a new object

A

remember to define the constructor property within that prototype object

Bird.prototype = {
  constructor: Bird, // define the constructor property
  numLegs: 2,
  eat: function() {
    console.log("nom nom nom");
  },
  describe: function() {
    console.log("My name is " + this.name); 
  }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

isPrototypeOf method:

A

When called on the original object, with the instance in parenthesis, it returns true if the instance belongs to the original object

Bird.prototype.isPrototypeOf(duck);
// returns true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

The prototype of a prototype is…

A

Object.prototype

17
Q

In a prototype chain, Bird is the ____ for duck, while duck is the ____. Object is a ___ for both Bird and duck. Object is a ____ for all objects in JavaScript.

A

supertype, subtype, supertype, supertype

Therefore, any object can use the hasOwnProperty method.

18
Q

Create an abstraction of a supertype that stores just the common properties for its subtypes

A

function Animal {};

Animal.prototype = {
constructor: Animal,
someFunc: function(){
//do stuff
}}

// we haven’t got to inheritance yet though

19
Q

Inherit Behaviors from a Supertype

A

let animal = Object.create(Animal.prototype); //better way to create a new instance based on Animal

20
Q

Set the Child’s Prototype to an Instance of the Parent

A
//then give it to the specific animal's prototype
Bird.prototype = Object.create(Animal.prototype);
21
Q

When an object inherits its prototype from another object, it also inherits the supertype’s ____

A

constructor property.

function Bird() { }
Bird.prototype = Object.create(Animal.prototype);
let duck = new Bird();
duck.constructor // function Animal(){...}
22
Q

manually set Bird’s constructor property to the Bird object (instead of the supertype Animal) – code:

A

Bird.prototype.constructor = Bird;

duck.constructor // function Bird(){…}

23
Q

A constructor function that inherits its prototype object from a supertype constructor function _____ its own methods

A

can still have

Dog.prototype.property = function() {//do shit}

24
Q

Override Inherited Methods

A

just write over the subtypes prototype with the same method name and give it a different definition

25
Q

Inheritance does not work well for ____

A

unrelated objects like Bird and Airplane. They can both fly, but a Bird is not a type of Airplane and vice versa.

26
Q

A mixin allows

A

other objects to use a collection of functions.

For unrelated objects, it’s better to use mixins.

let flyMixin = function(obj) {
  obj.fly = function() {
    console.log("Flying, wooosh!");
  }
};
27
Q

object’s property is considered public if

A

it can be accessed and changed outside of object’s definition

28
Q

The simplest way to make this public property private is by

A

creating a variable within the constructor function

function Bird() {
  let hatchedEgg = 10; // private variable
}
29
Q

In JavaScript, a function always has access to the context in which it was created. This is called

A

closure

30
Q

Understand the Immediately Invoked Function Expression (IIFE)

A
(function () {
  console.log("Chirp, chirp!");
})(); // this is an anonymous function expression that executes right away
// Outputs "Chirp, chirp!" immediately

Note that the function has no name and is not stored in a variable.

The two parentheses () at the end of the function expression cause it to be immediately executed or invoked.

31
Q

An immediately invoked function expression (IIFE) is often used to __

A

group related functionality into a single object or module.

We can group mixins into a module as follows:

let motionModule = (function () {
  return {//mixin 1
    glideMixin: function(obj) {
      obj.glide = function() {
        console.log("Gliding on the water");
      };
    },
//mixin 2
    flyMixin: function(obj) {
      obj.fly = function() {
        console.log("Flying, wooosh!");
      };
    }
  }
})(); // The two parentheses cause the function to be immediately executed