Skip to content

Instantly share code, notes, and snippets.

@storskegg
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save storskegg/b35fdb54049b8880aa79 to your computer and use it in GitHub Desktop.
Save storskegg/b35fdb54049b8880aa79 to your computer and use it in GitHub Desktop.
A Method to Produce Constants in ES5
// A small function that allows the creation of constants in ES5.
// Arguments:
// name <String>: The name of your constant
// value <Any>: The value of your constant
// isEnumerable <Bool>: Should your constant be enumerable? optional, defaults to true
// scope <Object>: The scope to which the constant is bound. optional, defaults to window (global)
var Const = function (name, value, isEnumerable, scope) {
"use strict";
if (isEnumerable === undefined) {
isEnumerable = true;
}
if (scope === undefined) {
scope = window;
}
var properties = {};
properties[name] = {
value: value,
enumerable: isEnumerable
};
Object.defineProperties(scope, properties);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment