class ClassName {
constructor(attr) {
this.var = attr;
}
// Note there is no ,
get priv() {return this._priv;}
set priv(newVal) {this._priv = newVal;}
static method() {...} // NOTE: can only be called from the class not the obj
};
Inheritance
// extends allows for access for the super classes methdos
class ClassName extends SuperClassName {
constructor(name, usesLitter) {
super(name); // constructor for super class
this._usesLitter = usesLitter;
}
}
Init
const objName = new ClassName(varName);
Objects
Create Object
const apple = {
color: 'green',
_priv: 'var that is private',
// we pretend that '_' means private
price: {
big: 25,
med: 20,
small: 15
},
methodName() {
return this.name
},
get priv() {return this._priv;},
set priv(newVal) {this._priv = newVal;},
// getter and setter examples
// refrence and assignments invoke getter and setter respectivly
// just pretend there is no '_'
};
// def of an object,
// const objects are still mutable
Calling Methods and Attributes
console.log(apple.price.med);
// calling a key
apple.methodName();
// calls a method
delete apple.color;
// deletes color key
function(apple);
// apple's refrence is passed, thus og apple is mutable within the functions scope
for (let key in obj) {
// key
// obj[key]
}