Index
Objects (OO)


Plain objects have a simple format as shown. They are basically key value pairs.

They can be used like dictionaries or objects as shown.

Plain objects do not have behaviors (methods) which is a primary difference between them and "full objects" (described below).

More...
rec = {
	'width': 10,
	'height': 5
};
//use like object
console.log(rec.width);
//or use like dict
console.log(rec['width']);
JavaScript supports (proper) objects via class declarations as shown.

More...
//Declaring
class Rectangle {
	//class body here (methods)
}
//Using
const rec = new Rectangle(10, 5);
console.log(`Area: ${rec.getArea()}`);
A method that constructs an object from a class.

It is common to set instance variables to passed params.

More...
constructor(aWidth, aHeight) {
	this.width = aWidth;
	this.height = aHeight;
}
//Usage
const rec = new Rectangle(8, 7);
A method available (usable) on the object instance.

More...
computeArea() {
	return this.width * this.height;
}
//Usage
a = rec.computeArea();
name = hasWidth
method params/args = aWidth
return value = in this case a boolan depending on comparision
method header = hasWidth(aWidth)
method body = the code inside the method (also called "source")

More...
hasWidth(aWidth) {
	return this.width === aWidth;
}
An instance method that sets and instance var (to passed param value).

More...
setWidth(aWidth) {
	this.width = aWidth;
}
//Usage
rec.setWidth(20);
An instance method that gets and returns and instance var value.

More...
getWidth() {
	return this.width;
}
//Usage
w = rec.getWidth();
An static method is prefixed by the keyword "static".

This method is associated with the class (e.g., Rectangle) rather than an object instance.

More...
static defaultWidth() {
	return 10;
}
//Usage ("Rectangle" is a class)
default = Rectangle.defaultWidth();
Messages are what make the object world go round.

Here we construct an object and send a variety of messages to it.

More...
const rec = new Rectangle(8, 7);
string = rec.toString();
rec.setWidth(20);
w = rec.getWidth();
Plain objects are simple -- basically data holders. They do not have behavior.

Proper js objects have the classic oo capabilities: data (ivars), behaviors (methods), inheritance, etc.
//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);

Functions


Here is a js traditional function expression.

More...
fct = function(a, b) {
		return a + b;
};
//usage
console.log(fct(10, 5));
Here is a js traditional function.

More...
//declaration
function multiply(a, b) {
	return a * b;
}
//usage
console.log(multiply(10, 5));
Although the declaration is a bit simpler, js coders tend to prefer the expression syntax.

The reason is that declared functions are system "globals". We only want to declare a "global" when we need it to be global (should be rare).

More...
//expression
fct = function(a, b) {
		return a + b;
};
//declaration (for global access)
function multiply(a, b) {
	return a * b;
}
Here is the js arrow function syntax.

More...
fct = (a, b) => {
		return a + b;
};
//or (abbrev form, for simple functions)
fct = (a, b) => a + b;
//usage
console.log(fct(10, 5));
A callback function (let's say "A") is a function that is passed to another function (let's say "B"). Fct "B" calls the passed function "A".

We first pass "addFct" as the callback. It is called in the target function -- result is 15.
We next pass "multiplyFct " as the callback. Result is 50.

More...
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);
Functions and methods are very similar.

The primary difference is that an instance method has the context of an object (shown by use of "this" in example).
//function
fct = function(a, b) {
		return a + b;
};

//method (in class Rectangle)
class Rectangle {
	//...
	getWidth() {
		return this.width;
	}
}

Basic


const vs let
"const" allows only one value be assigned to a var
"let" allows multiple assignments to a var

More...
const k = 8;
k = 7; //not allowed
let z = 5;
z = 4; //allowed
Strict Equality (===) vs Loose Equality(==)
Js coders generally use strict equality (===) as it is more intuitive.

Loose equality (shown here) can surprise. E.g, (0 == '') yields true.

More...
0 === ''
//false

0 == ''
//true