Dicts are objects, access members with ['name'] or .name
var person = {}; //{}
var person1 = new Object();
Iterate Over
Object.keys(p).map(k => )
Object.entries(p).map(([k, v]) => )
Object.values(p).map(v => )
for (let key of Object.keys(players)) {
//.......
}
General
Check in
'x' in {'x': 1} //true
Combine Objects
Objects/keys on the right override those to the left
{...x, ...y, ...z, a: 1}
Object.assign(x, y, z) //
Copy
{...x}
Object.assign({}, x)
Delete
delete myObject.prop;
Equality
=== will do ptr equality
Use lodash .isEqual for deep equality
Old Classes
function Person(name) {
this.name = name;
this.greeting = function() {
alert('Hi! I\'m ' + this.name + '.');
};
}
var person1 = new Person('Bob'); //new returns obj
var person2 = new Person('John'); //contains a new definition of gretting
To define functions out of scope,
function Test(a, b, c, d) {
// property definitions
}
// First method definition
Test.prototype.x = function() { ... };
// Second method definition
Test.prototype.y = function() { ... };
Can always add new values
person1.newv = "secretsss";
Prototypes?
objects can have a prototype object that all instances inherit from, everyone inherits from object prototype
Person.prototype
Object.prototype
Changes to prototype affect all people
Person.prototype.farewell = function() {
alert(this.name.first + ' has left. Bye for now!');
};
person1.farewell();
Create
Object.create makes a new object with the object given as its prototype, basically defining a subclass
var person3 = Object.create(person1);//copy constructor, (inherit from person1)
Inheritance
Its a mess, stupid javascript didn't want to do this
function Teacher(first, last, age, gender, interests, subject) {
Teacher.prototype = Object.create(Person.prototype); //inherit from Person.prototype
Teacher.prototype.constructor = Teacher; //you just replaced the constructor, add it back
Person.call(this, first, last, age, gender, interests); //.call allows you to pass this setting it
this.subject = subject;
}