Objects - SelfishHellfish/JS_Ref GitHub Wiki

Basic Notation:

var person = {key: value, key: value};, where value may be a string, number, function, etc. or another object.
can also initiate object by: var person = new Object(); or var newperson = Object.create(prototype);
and then append fields and properties to it. prototype may be null to avoid the root prototype

Using variables inside an object:

access object properties by specifying associated fields:
console.log(person.field); or console.log(person["field"]);
access object properties from within the object e.g. a function in the object working with the object's properties:
refer to property as this.property

Prototypes:

all objects in JS, by default, have a root prototype: Object.prototype. This is an object itself, but is associated with all primary objects created. It contains several of its own methods and grants all child objects access to these, a sort of inheritance. Some objects may be associated with multiple prototypes, resulting in a prototype chain, and JS will work its way through the levels of prototypes, searching for the called method in each, starting with the object and ending with the root prototype.
calling a function within a prototype that contains this.property will return the property of the object that called that function, which may be the child of that prototype.
get the prototype of an object, for logging or comparison:
Object.getPrototypeOf(person) or using dudner proto: person.__proto__ (unsafe/not officially supported)
A method is simply a function within an object.

Constructor Functions

Can be used to create objects. Useful when creating objects with multiple default fields. The resulting created objects are called instances. In creating an object from a constructor function, a prototype is also created e.g.:
function Person() {this.test="okay";this.exam="yessir";}; var person = new Person(); Person is the constructor function, person is the object, and Person.prototype is the prototype of person.
console.log(person instanceOf Person) will return true.

This

To specify the scope of this, use bind, call, or apply. bind binds value for future calling:
obj.obfn.bind(person, 'hello')();, whereas call and apply call the function instantly:
obj.obfn.call(person, 'hello'); and obj.obfn.apply(person, ['hello'])

.defineProperty

the value in using defineProperty vs objectname.method='property' is that it allows for property configuration. by default, creating properties with defineProperty results in them being read-only; to make them writable, include the writable field. the general form:
Object.defineProperty(objectname, 'method/keyname', {value: '', writable: true/false});
getters and setters may be used to configure what calling a property will return and what defining it will do e.g.:
Object.defineProperty(account, 'name', {get: function() {return this._name;}, set: function(name) {if (name=='Max') {this._name=name;}});
this allows for controlling/filtering the setting of properties

Default methods and properties

to delete a property:
delete objectname.fieldname;
check if field is in object:
console.log('fieldname' in objectname);
loop through an object:
for (var field in objectname) {console.log(objectname[field]}
objects cheat sheet