Skip to content

Instantly share code, notes, and snippets.

@ooflorent
Created February 4, 2015 09:57
Show Gist options
  • Save ooflorent/184b17b3523c9bf1d908 to your computer and use it in GitHub Desktop.
Save ooflorent/184b17b3523c9bf1d908 to your computer and use it in GitHub Desktop.
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