JSFiddle - React, Tailwind, and code Playground
by Elak
HTML
<dl id="fd"><dt>Initial Assignment</dt>
</dl>
<dl id="sd"><dt>After Value Change Attempt</dt>
</dl>
<dl id="td"><dt>After Adding More Enums</dt>
</dl>
CSS
dt {
font-weight: bold;
}
dd {
font-family:"Courier New", courier, monospaced;
font-size: 0.9em;
}
JavaScript
/**
* Define method on Object.prototype.
* Enum : declares type-safe properties on the active object, applies int value
* for each argument starting at 0 and incrementing by 1 for each.
* Does not set 'undefined', null, or empty string(''), but will still increment
* Accepts a number insert, to offset the value
* Example: obj.Enum('YELLOW',null,'','RED,'BLUE',10,'GREEN');
* obj.YELLOW == 0
* obj.RED == 3
* obj.BLUE == 4
* obj.GREEN == 11
* This should be compliant in:
* IE 9 +
* FF 4 +
* Chrome 5 +
* Opera 11.60 +
* Safari 5.1 +
*/
var odp = Object.defineProperty;
odp(Object.prototype, 'Enum', {
value: function () {
/* ignore array, certain entries will be treated as buffers, with no assignment
* */
var ignore = [undefined, null, ''],
/* n is our counter */
n = 0,
/* assign arguments to 'arg', helps the Closure Compiler minify this code
* better*/
args = arguments;
/* Loop through the args and start defining the values */
for (var i in args) {
/* If value is in ignore list, skip assignment and increment counter */
if (ignore.indexOf(args[i]) < 0) {
/* If value is a number of greater value than counter, set counter to that value
* and proceed without assignment */
if (typeof args[i] == "number") {
n = parseInt(args[i]);
} else {
try {
/* Assign our counter's value to the supplied arg as coerced String key value.
* Though it's rude to force convention, we'll do it anyway toUpperCase()
* Since we're forcing convention replace(" ","_")
*/
odp(this, String(args[i]).toUpperCase().replace(" ", "_"), {
value: parseInt(n),
enumerable:...