JavaScript Object Prototype is making me go nuts

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?

Hi @littlebirdy, could you give us more detail about what you’re confused about? I’m not sure if I understand what problem you’re having.

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.

Although this doesn’t answer your question directly, we published this recently: https://www.sitepoint.com/javascript-object-creation-patterns-best-practises/

1 Like

If you want to stay purely OO in Javascript then learn Babel or Typescript. Been using TypeScript for many months and I love it!

Yes indeed - put aside a half hour and work your way through this informative video on the subject.

3 Likes

Wow Thank you to everybody.

Actually I myself didn’t knew what to ask… Lol… I was going thru the articles and I understand none of them.

All your replies are going to help me.

Thank you and topic closed for now.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.