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