function Dog() {
this.name = "Rupert";
this.color = "brown";
this.numLegs = 4;
}
// Add your code below this line
let hound = new Dog();
Extend Constructors to Receive Arguments
function Dog(name, color, numLegs) {
this.name = name;
this.color = color;
this.numLegs = 4;
}
let terrier = new Dog("huahua", "blue");
Verify an Object’s Constructor with instanceof
/* jshint expr: true */
function House(numBedrooms) {
this.numBedrooms = numBedrooms;
}
// Add your code below this line
let myHouse = new House(5);
myHouse instanceof House;
Understand Own Properties
function Bird(name) {
this.name = name;
this.numLegs = 2;
}
let canary = new Bird("Tweety");
let ownProps = [];
// Add your code below this line
for(let property in canary) {
if(canary.hasOwnProperty(property)) {
ownProps.push(property);
}
}
Use Prototype Properties to Reduce Duplicate Code
function Dog(name) {
this.name = name;
}
Dog.prototype.numLegs = 2;
// Add your code above this line
let beagle = new Dog("Snoopy");
Iterate Over All Properties
function Dog(name) {
this.name = name;
}
Dog.prototype.numLegs = 4;
let beagle = new Dog("Snoopy");
let ownProps = [];
let prototypeProps = [];
// Add your code below this line
for(let property in beagle) {
if(beagle.hasOwnProperty(property)) {
ownProps.push(property);
} else {
prototypeProps.push(property);
}
}
Understand the Constructor Property
function Dog(name) {
this.name = name;
}
// Add your code below this line
function joinDogFraternity(candidate) {
if(candidate.constructor === Dog) {
return true;
} else {
return false;
}
}
Change the Prototype to a New Object
function Dog(name) {
this.name = name;
}
Dog.prototype = {
// Add your code below this line
numLegs: 4,
eat: function() {
console.log("mom, mom, mom");
},
describe: function() {
console.log("My name is " + this.name);
}
};
Remember to Set the Constructor Property when Changing the Prototype
function Dog(name) {
this.name = name;
}
// Modify the code below this line
Dog.prototype = {
constructor: Dog,
numLegs: 2,
eat: function() {
console.log("nom nom nom");
},
describe: function() {
console.log("My name is " + this.name);
}
};
Understand Where an Object’s Prototype Comes From
function Dog(name) {
this.name = name;
}
let beagle = new Dog("Snoopy");
// Add your code below this line
Dog.prototype.isPrototypeOf(beagle);
Understand the Prototype Chain
function Dog(name) {
this.name = name;
}
let beagle = new Dog("Snoopy");
Dog.prototype.isPrototypeOf(beagle); // => true
// Fix the code below so that it evaluates to true
Object.prototype.isPrototypeOf(Dog.prototype);
function Animal() { }
Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};
// Add your code below this line
let duck = Object.create(Animal.prototype); // Change this line
let beagle = Object.create(Animal.prototype); // Change this line
duck.eat(); // Should print "nom nom nom"
beagle.eat(); // Should print "nom nom nom"
Set the Child’s Prototype to an Instance of the Parent
function Animal() { }
Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};
function Dog() { }
// Add your code below this line
Dog.prototype = Object.create(Animal.prototype);
let beagle = new Dog();
beagle.eat(); // Should print "nom nom nom"
Reset an Inherited Constructor Property
function Animal() { }
function Bird() { }
function Dog() { }
Bird.prototype = Object.create(Animal.prototype);
Dog.prototype = Object.create(Animal.prototype);
// Add your code below this line
Bird.prototype.constructor = Bird;
Dog.prototype.constructor = Dog;
let duck = new Bird();
let beagle = new Dog();
Add Methods After Inheritance
function Animal() { }
Animal.prototype.eat = function() { console.log("nom nom nom"); };
function Dog() { }
// Add your code below this line
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function() {
console.log("Woof!");
}
// Add your code above this line
let beagle = new Dog();
beagle.eat(); // Should print "nom nom nom"
beagle.bark(); // Should print "Woof!"
Override Inherited Methods
function Bird() { }
Bird.prototype.fly = function() { return "I am flying!"; };
function Penguin() { }
Penguin.prototype = Object.create(Bird.prototype);
Penguin.prototype.constructor = Penguin;
// Add your code below this line
Penguin.prototype.fly = function() { return "Alas, this is a flightless bird."; };
// Add your code above this line
let penguin = new Penguin();
console.log(penguin.fly());
Use a Mixin to Add Common Behavior Between Unrelated Objects
let bird = {
name: "Donald",
numLegs: 2
};
let boat = {
name: "Warrior",
type: "race-boat"
};
// Add your code below this line
let glideMixin = function(obj) {
obj.glide = function() {
console.log("glide");
}
}
glideMixin(bird);
glideMixin(boat);
Use Closure to Protect Properties Within an Object from Being Modified Externally
function Bird() {
let weight = 15;
this.getWeight = function() {
return weight;
}
}
Understand the Immediately Invoked Function Expression (IIFE)
(function () {
console.log("A cozy nest is ready");
})();
Use an IIFE to Create a Module
let isCuteMixin = function(obj) {
obj.isCute = function() {
return true;
};
};
let singMixin = function(obj) {
obj.sing = function() {
console.log("Singing to an awesome tune");
};
};
let funModule = (function() {
return {
isCuteMixin: function(obj) {
obj.isCute = function() {
return true;
};
},
singMixin: function(obj) {
obj.sing = function() {
console.log("Singing to an awesome tune");
};
}
}
})();
Object Oriented ProgrammingCreate a Basic JavaScript Objectlet dog = { 'name' : "xiaohua", 'numLegs': 4};Use Dot Notation to Access the Properties of an Objectlet dog = { name: "Spot", n...