{"id":1054,"date":"2012-02-18T16:34:34","date_gmt":"2012-02-18T21:34:34","guid":{"rendered":"http:\/\/yuguangzhang.com\/blog\/?p=1054"},"modified":"2012-02-18T16:46:10","modified_gmt":"2012-02-18T21:46:10","slug":"enumerables-in-javascript","status":"publish","type":"post","link":"http:\/\/yuguangzhang.com\/blog\/enumerables-in-javascript\/","title":{"rendered":"Enumerables in JavaScript"},"content":{"rendered":"<p>Creating clean, readable code is a primary imperative when working with large systems. Today, I have factored the knockout view model from a single object for both projects into an inheritance based hierarchy. Now in the middle of it, I&#8217;m replacing a <em>programmingLanguage<\/em>\u00a0variable with a language class that also handles the language for the style sheet (LESS, SCSS) and document (Jade, ECO).<br \/>\n[cc lang=&#8221;javascript&#8221;]<br \/>\nvar Language = Class.$extend({<br \/>\n    __init__: function () {<br \/>\n        LANGUAGE = {<br \/>\n            PYTHON: 0,<br \/>\n            JAVASCRIPT: 1,<br \/>\n            LESS: 2,<br \/>\n            CSS: 3,<br \/>\n            HTML: 4<br \/>\n        };<br \/>\n        LANGUAGE_TYPE = {<\/p>\n<p>            STYLE: 1,<br \/>\n        }<br \/>\n    }<br \/>\n})<br \/>\n[\/cc]<br \/>\nThen I paused and thought it didn&#8217;t seem right. I remember Enums were more terse in C. So I decided to us an <em>Enum<\/em> class.<br \/>\n[cc lang=&#8221;javascript&#8221;]<br \/>\nfunction Enum() {<br \/>\n    var obj = new Object();<br \/>\n    for (var i = 0; i < arguments.length; i++) {\n        obj[arguments[i]] = i;\n    }\n    return obj;\n}\n\nvar Language = Class.$extend({\n    __init__: function () {\n        LANGUAGE = Enum('PYTHON', 'JAVASCRIPT', 'LESS', 'CSS', 'HTML');\n        LANGUAGE_TYPE = Enum('SCRIPT', 'STYLE', 'DOCUMENT');\n    }\n})\n[\/cc]\nNow I like that much better. It's like automatic numbering, except I don't have to read it. \n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating clean, readable code is a primary imperative when working with large systems. Today, I have factored the knockout view model from a single object for both projects into an inheritance based hierarchy. Now in the middle of it, I&#8217;m replacing a programmingLanguage\u00a0variable with a language class that also handles the language for the style [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[23,146],"tags":[133],"class_list":["post-1054","post","type-post","status-publish","format-standard","hentry","category-programming","category-pythonfiddle","tag-javascript"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/posts\/1054","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/comments?post=1054"}],"version-history":[{"count":0,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/posts\/1054\/revisions"}],"wp:attachment":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/media?parent=1054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/categories?post=1054"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/tags?post=1054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}