Last active
August 29, 2015 14:06
-
-
Save bahelms/3fbbb5b9e89638f9df5d to your computer and use it in GitHub Desktop.
OOP JavaScript notes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reflection | |
typeof 100 => Number | |
object instanceof Object => true/false | |
Array.isArray(array_var) => true/false | |
// Detecting object properties | |
"property" in object => true/false // checks own and prototype | |
object.hasOwnProperty("property") => true/false | |
Object.keys(object) // returns own properties only | |
for (var property in object) {} // loops over enumerable own and prototype properties | |
// this manipulation | |
function.call(this_object, args, ...); // executes function with new this | |
function.apply(this_object, [args, ...]); // executes function with new this | |
function.bind(this_object, permanent_overriding_args, ...); // returns new function, permanently overwriting arg values | |
// Misc | |
delete object.property // deletes own property from object | |
// Objects | |
var object = { | |
this._prop, // "private" data property | |
get prop() {}, // accessor property | |
set prop(arg) {} // accessor property | |
} | |
Object.defineProperty(object, property, descriptor) | |
descriptor === { | |
enumerable: t/f, // data/accessor | |
configurable: t/f, // data/accessor; whether property can be changed | |
writable: t/f, // data only; whether property value can be changed | |
value: "value of property", // data only | |
get: function() {}, // accessor only | |
set: function() {} // accessor only | |
} | |
// All t/f default to true | |
Object.defineProperties(object, { | |
prop_name: descriptor_object, | |
prop2_name: descriptor_object, | |
etc.. | |
}) | |
Object.create(prototype_object, { | |
prop_name: descriptor_object, | |
prop2_name: descriptor_object, | |
etc.. | |
}) | |
Object.getOwnPropertyDescriptor(object, property) => retuns descriptor object | |
Object.preventExtensions(object) => prevents the addition of properties; Object.isExtensible(object) | |
Object.seal(object) => [[Extensible]] and [[Configurable]] are set to false; Object.isSealed(object) | |
Object.freeze(object) => sealed and property values can't be changed; Object.isFrozen(object)k | |
// Class | |
function Constructor() { | |
this.data_properties, | |
etc... | |
} | |
Constructor.prototype = { | |
constructor: Constructor | |
method: function() {}, | |
method2: function() {}, | |
etc... | |
} | |
// Inheritance | |
Constructor.prototype = prototype_object; // ex: new OtherConstructor(); Object.create(OtherConstructor.prototype, {}) | |
Constructor.prototype.constructor = Constructor; | |
// Constructor Stealing | |
function Constructor() { | |
OtherConstructor.call(this, args...); | |
} | |
Constructor.prototype = new OtherConstructor(); | |
Constructor.prototype.constructor = Constructor; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment