Jump to content

JavaScript: Difference between revisions

859 bytes added ,  20 January 2020
Line 28: Line 28:
console.log(match[1], match[2]);
console.log(match[1], match[2]);
console.table(match);
console.table(match);
===Classes===
Traditionally, classes are done using functions in JavaScript.<br>
{{hidden | Traditional JS Classes |
<syntaxhighlight lang="javascript">
function Rectangle(height, width) {
  this.height = height;
  this.width = width;
}
// To extend some Shape class
Rectangle.prototype = Object.create(Shape.prototype);
// To add to the prototype
Object.assign(Rectangle.prototype, {
  getSize: function() {
    return this.height * this.width;
  }
});
</syntaxhighlight>
}}
[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes Mozilla Reference]<br>
ES2015 adds syntax for classes in JavaScript.<br>
<syntaxhighlight lang="javascript">
class Rectangle extends Shape {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
  getSize() {
    return this.height * this.width; 
  }
}
</syntaxhighlight>


</syntaxhighlight>
</syntaxhighlight>