Created
June 22, 2014 07:02
-
-
Save toctan/c775471b1d190d0e0d7d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// An object is a dynamic collection of properties. | |
// Every property has a key string that is unique within that object. | |
// get: | |
object.name; | |
object[expression]; | |
// set: | |
object.name = value; | |
object[expression] = value; | |
// delete | |
delete object.name; | |
delete object[expression]; | |
// A property is a named collection of attributes. | |
// There are two kinds of properties: | |
// Data properties: | |
// - value | |
// - writeable | |
// - enumerable | |
// - configurable | |
// Object literals: | |
// An expression notation for creating objects. | |
var my_object = {foo: bar}; | |
// equivalent to | |
var my_object = Object.defineProperties( | |
Object.create(Object.prototype), { | |
foo: { | |
value: bar, | |
writeable: true, | |
enumerable: true, | |
configurable: true | |
} | |
}); | |
// Accessor properties: | |
// - get: function () { return value; } | |
// - set: function (value) {} | |
Object.defineProperty(my_object, 'inch', { | |
get: function () { | |
return this.mm / 25.4; | |
}, | |
set: function (value) { | |
this.mm = value * 25.4; | |
}, | |
enumerable: true | |
}); | |
// Objects also have an extensible attribute | |
// custom immutable object | |
Object.isExtensible(); | |
Object.preventExtensions(); | |
Object.seal(); | |
Object.isSealed(); | |
Object.freeze(); | |
Object.isFrozen(); | |
// New in ES5 | |
Object.defineProperty(object, key, descriptor); | |
Object.defineProperties(object, object_of_descriptors); | |
Object.getOwnPropertyDescriptor(object, key); | |
Object.getOwnPropertyNames(object); | |
Object.keys(object); | |
// Working with prototypes | |
// Delegation, also called differential inheritance | |
// 1. Make an object that you like. | |
// 2. Create new instances that inherit from that object. | |
// 3. Customize the new objects. | |
// prototype: object or null | |
Object.create(object, properties); | |
Object.getPrototypeOf(object); | |
// new prefix operator | |
function my_new (func, arguments) { | |
var that = Object.create(func.properties), | |
result = func.apply(that, arguments); | |
return (typeof return === 'object' && result) || that; | |
} | |
// Where JavaScript didn't get it quite right | |
// Accidental collision | |
// Solution in ES5: Object.create(null) | |
function bump_count (word) { // fails when word === 'constructor' | |
if (word_count[word]) { | |
word_count[word] += 1; | |
} else { | |
word_count[word] = 1; | |
} | |
} | |
function bump_count (word) { | |
if (typeof word_count[word] === 'number') { | |
word_count[word] += 1; | |
} else { | |
word_count[word] = 1; | |
} | |
} | |
// The for in problem | |
// Solution in ES5: | |
// - set the enumerable attribute to false when adding methods to prototypes | |
// - Object.keys(object) | |
for (name in object) { // fails if object has a hasOwnProperty property | |
if (object.hasOwnProperty(name)) { | |
// ... | |
} | |
} | |
for (name in object) { | |
if (Object.hasOwnProperty.call(object, name)) { | |
// ... | |
} | |
} | |
// Keys must be strings | |
function make_sealer() { | |
var boxes = [], values = []; | |
return { | |
sealer: function (value) { | |
var box, i = boxes.length; | |
box = {}; | |
boxes[i] = box; | |
values[i] = value; | |
return box; | |
}, | |
unsealer: function (box) { | |
return values[boxes.indexOf(box)]; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment