// prototype in javascript class
// To declare a class, you use the class keyword with the name of the class ("Rectangle" here).
class Rectangle {
constructor(w, h) {
this.w = w;
this.h = h;
}
}
// adds an 'area' method to the Rectangle class' as prototype
Rectangle.prototype.area = function () {
return this.w * this.h;
};
// create an instance of class
const findAreaOfRectangle = new Rectangle(8,3);
// invoke the area method
console.log(findAreaOfRectangle.area()); // Output: 24
A prototype simply does a method of that specific class, it is called a method because it is inside the class but after all it does a function
Code Example |
---|
Javascript :: generate history logs |
:: openai giving me a 401 |
:: |
:: |
:: onclick clear input field javascript |
:: |
Javascript :: |
:: |
Javascript :: |
Javascript :: handle stream javascript |
Javascript :: |
:: |
Javascript :: |
:: |
:: |
:: |
:: react-native-page-control |
:: |
Javascript :: re-resizable react example |
Javascript :: |
Javascript :: js extend list |
:: |
:: |
Javascript :: |
Javascript :: |
Javascript :: erc721 abi json |
:: |
:: |
:: devexpress image collection |
:: |