Implementing a class in JavaScript

let’s implement a class called FeedDog in JavaScript, that has an object inside which changes when a function is called!

var FeedDog=function(treat){var obj={treatSize:treat};
return obj;

here, FeedDog is a class which has a method called increaseTreat, which increases TreatSize by one.

we could execute it like:

var x=FeedDog(5);
console.log(x.treatSize); //will print 5
console.log(x.treatSize); //will print 6 (cause x.increaseTreat was called before);

that’s it, you have a class up and running in javascript! 🙂

About the author


Add comment

By sarabpreet

Latest from Sarabpreet