Objects & Classes

Classes

Create a class

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

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


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


const objFactory = (att1, att2) =>
{
	return {
		att1: att1,
		att2: att2,
		method() {
			console.log('logggggggg');
		}
	};
};

short hand way of looking at object values

const rubiksCubeFacts = {
  possiblePermutations: '43,252,003,274,489,856,000',
  invented: '1974',
  largestCube: '17x17x17'
};

const {possiblePermutations, invented, largestCube} = rubiksCubeFacts;

console.log(possiblePermutations); 
// '43,252,003,274,489,856,000'
console.log(invented);
// '1974'
console.log(largestCube); 
// '17x17x17'

short hand way of creating new attributes

const activity = 'Surfing';
const beach = { activity };

console.log(beach); // { activity: 'Surfing' }