JSFiddle - React, Tailwind, and code Playground
JavaScript
// Using the prototype.
function Size(width, height) {
this.width = width;
this.height = height;
}
Size.prototype.area = function() {
return this.width * this.height;
};
// Using instance methods and fields.
function Size2(width, height) {
this.width = width;
this.height = height;
this.area = function() {
return this.width * this.height;
}
}
var s = new Size(5, 10);
var s2 = new Size2(5, 10);
print("Size");
print(s.area());
print("Size 2");
print(s2.area());
// Looks identical, but lets use the reference equality operator to test things:
var s3 = new Size2(5, 10);
var s4 = new Size(5, 10);
print(s2.area == s3.area); // oops, it's false
print(s.area === s4.area);
function print(x) {
document.body.innerHTML += "<p>" + x + "</p>";
}