Created
February 4, 2015 09:57
-
-
Save ooflorent/184b17b3523c9bf1d908 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
export const READ_ONLY = 1 | |
export const DONT_ENUM = 2 | |
export const DONT_DELETE = 4 | |
export function InstallFunctions(object, attributes, functions) { | |
for (let i = 0; i < functions.length; i += 2) { | |
let field = functions[i] | |
let value = functions[i + 1] | |
AddNamedProperty(object, field, value, attributes) | |
} | |
} | |
export function InstallConstants(object, constants) { | |
let attributes = DONT_ENUM | DONT_DELETE | READ_ONLY | |
for (let i = 0; i < constants.length; i += 2) { | |
let field = constants[i] | |
let value = constants[i + 1] | |
AddNamedProperty(object, field, value, attributes) | |
} | |
} | |
export function SetUpLockedPrototype(constructor, fields, methods) { | |
let prototype = constructor.prototype | |
for (let i = 0; i < fields.length; i++) { | |
AddNamedProperty(prototype, fields[i], undefined, DONT_ENUM | DONT_DELETE) | |
} | |
InstallFunctions(prototype, DONT_ENUM | DONT_DELETE | READ_ONLY, methods) | |
} | |
function AddNamedProperty(object, field, value, attributes) { | |
Object.defineProperty(object, field, { | |
value: value, | |
configurable: 0 === (attributes & DONT_DELETE), | |
enumerable: 0 === (attributes & DONT_ENUM), | |
writable: 0 === (attributes & READ_ONLY) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment