4.14 Prototypes Part 3¶
Modifying Constructors 2¶
Task 1: Delete the fullName
method from the User.prototype
below. Hint: use delete User.prototype.fullName
function User(fstName, lstName){
this.firstName = fstName;
this.lastName = lstName;
}
User.prototype.fullName = function(){
return (this.firstName + " " + this.lastName);
}
console.log(User.prototype);
console.log(".................")
delete User.prototype.fullName;
console.log(User.prototype);
The result is as follows:
{fullName: ƒ, constructor: ƒ}
.................
{constructor: ƒ}
Task 2: Delete the languageVerbose
method from the Epic.prototype
below.
function Epic(name, author, lang){
this.name = name;
this.author = author;
this.language = lang;
}
Epic.prototype.languageVerbose = function(){
return `${this.name} was written in ${this.language}.`
}
console.log(Epic.prototype);
console.log(".............................")
delete Epic.prototype.languageVerbose;
console.log(Epic.prototype);
The result is as follows:
{languageVerbose: ƒ, constructor: ƒ}
.............................
{constructor: ƒ}
Task 3: Delete the category
property from the Video.prototype
below.
function Video(title, lang){
this.title = title;
this.language = lang;
}
Video.prototype.category = "Education";
console.log(Video.prototype);
console.log(".....................")
delete Video.prototype.category;
console.log(Video.prototype);
The result is as follows:
{category: "Education", constructor: ƒ}
.....................
{constructor: ƒ}
Task 4: Delete the brand
property from the MarkerPen.prototype
below.
function MarkerPen(color, price){
this.color = color;
this.price = price;
}
MarkerPen.prototype.brand = "Claro";
console.log(MarkerPen.prototype);
console.log(".....................")
delete MarkerPen.prototype.brand;
console.log(MarkerPen.prototype);
The result is as follows:
{brand: "Claro", constructor: ƒ}
.....................
{constructor: ƒ}
Task 5: Delete the course
property from the Section.prototype
below.
function Section(title, desc){
this.title = title;
this.description = desc;
}
Section.prototype.course = "JavaScript Course";
console.log(Section.prototype);
console.log(".....................")
delete Section.prototype.course;
console.log(Section.prototype);
The result is as follows:
{course: "JavaScript Course", constructor: ƒ}
.....................
{constructor: ƒ}
Modifying Prototypes 2
You can delete a property or a method from an object prototype using delete constructorName.prototype.key
Extra Practice¶
Task 6: Build a lesson constructor. Hint: Use the below lesson object.
/*
var lesson = {
title: null,
difficultyLevel: null
};
*/
function Lesson(title, diffLev){
this.title = title;
this.difficultyLevel = diffLev;
}
Task 7: Refer to task 6; add section
property to Lesson.prototype
. Set the section value to Section 1
.
function Lesson(title, diffLev){
this.title = title;
this.difficultyLevel = diffLev;
}
Lesson.prototype.section = "Section 1";
console.log(Lesson.prototype);
The result is: {section: "Section 1", constructor: ƒ}
Task 8: Refer to task 7; add about
method to Lesson.prototype
. Set the about value to {title} is {difficultyLevel} and it comes under {section} discussion
function Lesson(title, diffLev){
this.title = title;
this.difficultyLevel = diffLev;
}
Lesson.prototype.section = "Section 1";
Lesson.prototype.about = function(){
return `The difficulty level of ${this.title} is ${this.difficultyLevel} and it comes under ${this.section} discussion.`;
}
var lesson1 = new Lesson("Array Helpers", "Normal");
var lesson2 = new Lesson("Objects", "Easy");
console.log(lesson1.about());
console.log(lesson2.about());
The result is as follows:
The difficulty level of Array Helpers is Normal and it comes under Section 1 discussion.
The difficulty level of Objects is Easy and it comes under Section 1 discussion.
Task 9: Refer to task 8; display all the added properties and methods to the lesson.prototype
.
function Lesson(title, diffLev){
this.title = title;
this.difficultyLevel = diffLev;
}
Lesson.prototype.section = "Section 1";
Lesson.prototype.about = function(){
return `The difficulty level of ${this.title} is ${this.difficultyLevel} and it comes under ${this.section} discussion.`;
}
for (var prop in Lesson.prototype){
console.log(`${prop}:${Lesson.prototype[prop]}`);
}
The result is as follows:
section:Section 1
about:function(){
return `The difficulty level of ${this.title} is ${this.difficultyLevel} and it comes under ${this.section} discussion.`;
}
Task 10: Refer to Task 9; delete about
method from Lesson.prototype
.
function Lesson(title, diffLev){
this.title = title;
this.difficultyLevel = diffLev;
}
Lesson.prototype.section = "Section 1";
Lesson.prototype.about = function(){
return `The difficulty level of ${this.title} is ${this.difficultyLevel} and it comes under ${this.section} discussion.`;
}
for (var prop in Lesson.prototype){
console.log(`${prop}:${Lesson.prototype[prop]}`);
}
console.log("......................");
delete Lesson.prototype.about;
for (var prop in Lesson.prototype){
console.log(`${prop}:${Lesson.prototype[prop]}`);
}
The result is as follows:
section:Section 1
about:function(){
return `The difficulty level of ${this.title} is ${this.difficultyLevel} and it comes under ${this.section} discussion.`;
}
......................
section:Section 1
Task 11: Build a quote constructor. Hint: Use the below quote object.
/*
var quote = {
author: null,
source: null,
theWord: null
};
*/
function Quote(author, src, word){
this.author = author;
this.source = src;
this.theWord = word;
}
Task 12: Refer to task 11; add date
property to Quote.prototype. Set the date value to Date.now()
.
function Quote(author, src, word){
this.author = author;
this.source = src;
this.theWord = word;
}
Quote.prototype.date = Date.now();
console.log(Quote.prototype);
The result is: {date: 1601195465160, constructor: ƒ}
Task 13: Refer to task 12; add mentions
property to Quote.prototype. Set the mentions value to ["book1", "book2", "book3"]
.
function Quote(author, src, word){
this.author = author;
this.source = src;
this.theWord = word;
}
Quote.prototype.date = Date.now();
Quote.prototype.mentions = ["book1", "book2", "book3"];
console.log(Quote.prototype);
The result is: {date: 1601195642503, mentions: Array(3), constructor: ƒ}
Task 14: Refer to task 13; add category
property to Quote.prototype. Set the category value to religious
.
function Quote(author, src, word){
this.author = author;
this.source = src;
this.theWord = word;
}
Quote.prototype.date = Date.now();
Quote.prototype.mentions = ["book1", "book2", "book3"];
Quote.prototype.category = "religious";
console.log(Quote.prototype);
The result is: {date: 1601195713013, mentions: Array(3), category: "religious", constructor: ƒ}
Task 15: Refer to task 14; display all the added properties and methods to the Quote.prototype.
function Quote(author, src, word){
this.author = author;
this.source = src;
this.theWord = word;
}
Quote.prototype.date = Date.now();
Quote.prototype.mentions = ["book1", "book2", "book3"];
Quote.prototype.category = "religious";
for (var prop in Quote.prototype){
console.log(`${prop}:${Quote.prototype[prop]}`);
}
The result is as follows:
date:1601195806427
mentions:book1,book2,book3
category:religious
Task 16: Refer to task 15; delete category
property from Quote.prototype
.
function Quote(author, src, word){
this.author = author;
this.source = src;
this.theWord = word;
}
Quote.prototype.date = Date.now();
Quote.prototype.mentions = ["book1", "book2", "book3"];
Quote.prototype.category = "religious";
for (var prop in Quote.prototype){
console.log(`${prop}:${Quote.prototype[prop]}`);
}
console.log(".......");
delete Quote.prototype.category;
for (var prop in Quote.prototype){
console.log(`${prop}:${Quote.prototype[prop]}`);
}
The result is as follows:
date:1601195925683
mentions:book1,book2,book3
category:religious
.......
date:1601195925683
mentions:book1,book2,book3