Hi, Today whole from morning and its mid night now, I read articles after articles about Object Prototype and still I don’t understand anything of it. Both Theory parts and coding parts of the articles are passing through over my head.
Yes I understood that Creating Objects with the help of Prototype leads to more faster creation of Objects and Multiple Objects can be created using the same Prototype and other than this I don’t understand anything of Prototype. Lets not talk about Inheritance at the moment.
I got this code from a website and I understand this code.
Creating an Object Using Prototype Pattern
function Fruit () {
}
Fruit.prototype.color = "Yellow";
Fruit.prototype.sweetness = 7;
Fruit.prototype.fruitName = "Generic Fruit";
Fruit.prototype.nativeToLand = "USA";
Fruit.prototype.showName = function () {
console.log("This is a " + this.fruitName);
}
Fruit.prototype.nativeTo = function () {
console.log("Grown in:" + this.nativeToLand);
}
var mangoFruit = new Fruit ();
mangoFruit.showName(); // This is a Generic Fruit
mangoFruit.nativeTo(); // Grown in:USA
Is their anything I still need to understand other than this code?
There are quite a few ways to create objects but I’ve never seen someone choosing one method from another due to performance reasons. Keep in mind that Javascripts are run on client’s machine. From what I know, you use prototypical object when you need inheritance. I’ve been doing web dev over 10+ years and never seen it use natively. Typically, you would use another library like jquery, typescript, and etc… and use their class models.
I’m not sure how you are learning this topic but look it up in Youtube as they’ll show you in live demo.