This code has to be modified
1) Everytime you create Study object , you create courselevel and Enquiry object
2)We need to pull out these methods to impelement prototype pattern
2)We need to pull out these methods to impelement prototype pattern
let Study = function (course, level) {
this.course = course;
this.level = level;
this.courseLevel = function () {
console.log(this.course + " is for " + this.level);
};
this.Enquiry = function () {
console.log("I just want to have enquiry. I am yet to think of enrolling...");
};
};
let study1 = new Study("Javascript", "Beginners");
let study2 = new Study("Advanced Javascript", "Professionals");
let study3 = new Study("ES new concepts", "Proficients");
let study4 = new Study("Typescript", "Beginners");
study1.courseLevel();
study2.courseLevel();
study3.courseLevel();
study4.Enquiry();
//Output
// Javascript is for Beginners
// Advanced Javascript is for Professionals
// ES new concepts is for Proficient
Modified code
1)Encapsulation of properties that an object links to
2)We have pulled out the courseLevel and enquiry function from Study function
3)We have created separate prototype for -> courseLevel and enquiry
Advantage ??
4)Every time we create study object ...we are not creating the courseLevel and enquiry object as it was earlier...
5)If you see study4 doesn't need to know the courseLevel...It just invokes enquiry..
6)As per earlier code ,study4 would still create courseLevel object while creating new Study(), just an overhead...
7)This is the advantage of prototype pattern.......
2)We have pulled out the courseLevel and enquiry function from Study function
3)We have created separate prototype for -> courseLevel and enquiry
Advantage ??
4)Every time we create study object ...we are not creating the courseLevel and enquiry object as it was earlier...
5)If you see study4 doesn't need to know the courseLevel...It just invokes enquiry..
6)As per earlier code ,study4 would still create courseLevel object while creating new Study(), just an overhead...
7)This is the advantage of prototype pattern.......
let Study = function (course, level) {
this.course = course;
this.level = level;
};
Study.prototype.courseLevel = function () {
console.log(this.course + " is for " + this.level);
};
Study.prototype.enquiry = function () {
console.log("I just want to have enquiry. I am yet to think of enrolling...");
};
let study1 = new Study("Javascript", "Beginners");
let study2 = new Study("Advanced Javascript", "Professionals");
let study3 = new Study("ES new concepts", "Proficients");
let study4 = new Study("Typescript", "Beginners");
study1.courseLevel();
study2.courseLevel();
study3.courseLevel();
study4.enquiry();
//Output
// Javascript is for Beginners
// Advanced Javascript is for Professionals
// ES new concepts is for Proficient
Classes from ES2015...
1)We have classes from ES2015
2)Object oriented way
2)Object oriented way
//**IN ECMASCRIPT WE NOW USE CLASSES INSTEAD OF FUNCTIONS.... */
class Study {
constructor(course, level) {
this.course = course;
this.level = level;
}
courseLevel = function () {
console.log(this.course + " is for " + this.level);
};
enquiry = function () {
console.log("I just want to have enquiry. I am yet to think of enrolling...");
};
}
let study1 = new Study("Javascript", "Beginners");
let study2 = new Study("Advanced Javascript", "Professionals");
let study3 = new Study("ES new concepts", "Proficients");
let study4 = new Study("Typescript", "Beginners");
study1.courseLevel();
study2.courseLevel();
study3.courseLevel();
study4.enquiry();
//Output
// Javascript is for Beginners
// Advanced Javascript is for Professionals
// ES new concepts is for Proficient
//I just want to have enquiry. I am yet to think of enrolling...