rec = { 'width': 10, 'height': 5 }; //use like object console.log(rec.width); //or use like dict console.log(rec['width']);
//Declaring class Rectangle { //class body here (methods) } //Using const rec = new Rectangle(10, 5); console.log(`Area: ${rec.getArea()}`);
constructor(aWidth, aHeight) { this.width = aWidth; this.height = aHeight; } //Usage const rec = new Rectangle(8, 7);
computeArea() { return this.width * this.height; } //Usage a = rec.computeArea();
hasWidth(aWidth) {
return this.width === aWidth;
}
setWidth(aWidth) { this.width = aWidth; } //Usage rec.setWidth(20);
getWidth() { return this.width; } //Usage w = rec.getWidth();
static defaultWidth() { return 10; } //Usage ("Rectangle" is a class) default = Rectangle.defaultWidth();
const rec = new Rectangle(8, 7); string = rec.toString(); rec.setWidth(20); w = rec.getWidth();
//plain object rec = { 'width': 10, 'height': 5 }; //class class Rectangle { //class body here (methods) } //proper objects (constructed from class) const rec = new Rectangle(10, 5);
fct = function(a, b) {
return a + b;
};
//usage
console.log(fct(10, 5));
//declaration function multiply(a, b) { return a * b; } //usage console.log(multiply(10, 5));
//expression fct = function(a, b) { return a + b; }; //declaration (for global access) function multiply(a, b) { return a * b; }
fct = (a, b) => { return a + b; }; //or (abbrev form, for simple functions) fct = (a, b) => a + b; //usage console.log(fct(10, 5));
let fct, addFct, multiplyFct; fct = (a, b, operationFct) => { console.log(operationFct(a, b)); }; addFct = (num1, num2) => num1 + num2; fct(10, 5, addFct); multiplyFct = (num1, num2) => num1 * num2; fct(10, 5, multiplyFct);
//function fct = function(a, b) { return a + b; }; //method (in class Rectangle) class Rectangle { //... getWidth() { return this.width; } }
const k = 8; k = 7; //not allowed let z = 5; z = 4; //allowed
0 === '' //false 0 == '' //true