Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

prototype in javascript class

// 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
 
PREVIOUS NEXT
Tagged: #prototype #javascript #class
ADD COMMENT
Topic
Name
7+8 =