Object Oriented Programming Flashcards
create a basic object
var dog = { name: "Fido", numLegs: 4 }
call object’s properties
object.property
Methods are properties that are
functions
Constructors are defined with _____ to distinguish them from other functions that are not constructors.
a capitalized name
Verify an Object’s Constructor with
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
every instance of Object will have its own copy of these properties T/F?
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); } }
Properties in the prototype are shared…
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 to add property to prototype
Object.prototype.property = value;
define this outside of the object constructor;
Own properties are defined _____. And prototype properties are defined _____
directly on the object instance itself
on the prototype
the constructor property of an object is ____
is a reference to the constructor function that created the instance
The advantage of the constructor property is that it’s possible to ____
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 to add prototype properties at scale, not 1 by 1
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); } };
There is one crucial side effect of manually setting the prototype to a new object:
It erases the constructor property
To fix the issue of erasing the constructor property when manually setting the prototype to a new object
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); } };
isPrototypeOf method:
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
The prototype of a prototype is…
Object.prototype
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.
supertype, subtype, supertype, supertype
Therefore, any object can use the hasOwnProperty method.
Create an abstraction of a supertype that stores just the common properties for its subtypes
function Animal {};
Animal.prototype = { constructor: Animal, someFunc: function(){ //do stuff }}
// we haven’t got to inheritance yet though
Inherit Behaviors from a Supertype
let animal = Object.create(Animal.prototype); //better way to create a new instance based on Animal
Set the Child’s Prototype to an Instance of the Parent
//then give it to the specific animal's prototype Bird.prototype = Object.create(Animal.prototype);
When an object inherits its prototype from another object, it also inherits the supertype’s ____
constructor property.
function Bird() { } Bird.prototype = Object.create(Animal.prototype); let duck = new Bird(); duck.constructor // function Animal(){...}
manually set Bird’s constructor property to the Bird object (instead of the supertype Animal) – code:
Bird.prototype.constructor = Bird;
duck.constructor // function Bird(){…}
A constructor function that inherits its prototype object from a supertype constructor function _____ its own methods
can still have
Dog.prototype.property = function() {//do shit}
Override Inherited Methods
just write over the subtypes prototype with the same method name and give it a different definition