{"id":356,"date":"2009-05-08T22:14:39","date_gmt":"2009-05-09T02:14:39","guid":{"rendered":"http:\/\/eligrey.com\/?p=356"},"modified":"2009-05-08T22:14:39","modified_gmt":"2009-05-09T02:14:39","slug":"ecmascript-accessors","status":"publish","type":"post","link":"https:\/\/eligrey.com\/blog\/ecmascript-accessors\/","title":{"rendered":"ECMAScript 5 accessors"},"content":{"rendered":"<p>A while ago, I created a JavaScript library named <a href=\"http:\/\/github.com\/eligrey\/Xccessors\">Xccessors<\/a>, which implemented the legacy non-standard accessor (getter and setter) methods in IE8. I initially created two different libraries and decided that the library that implemented the legacy methods would be more useful at the time due to no changes needed in a JavaScript program\u2019s code to add accessor support for IE. I never released the second one that implements the ECMAScript 5 (formerly 3.1) standard accessor methods so I\u2019m releasing it now. There can\u2019t be two different libraries with the same \u201cXccessors\u201d name, so I am also renaming the libraries accordingly to what they implement. The new names are Xccessors Legacy and Xccessors Standard.<\/p>\n<p>Here are two examples of using <code>Object.defineProperty<\/code> and <code>Object.getOwnPropertyDescriptor<\/code>:<\/p>\n<h4>Using accessors<\/h4>\n<pre lang=\"javascript\">(function() {\n\/\/ this creates a document.foo accessor\n    var foo = 0;\n    Object.defineProperty(document, \"foo\", {\n        get: function() { return foo },\n        set: function(x) { return foo += x }\n    });\n})();\n\ndocument.foo = 5;\n(document.foo = 4) === 9;\n\/\/Object.getOwnPropertyDescriptor(document, \"foo\") is {set:[...], get:[...]}<\/pre>\n<h4>Setting a property<\/h4>\n<pre lang=\"javascript\">\/\/ this is the equivalent of window.foo = \"bar\";\nObject.defineProperty(window, \"foo\", {value: \"bar\"});\nwindow.foo === \"bar\";\nObject.getOwnPropertyDescriptor(window, \"foo\").value === \"bar\";\nwindow.x = 0;\nObject.getOwnPropertyDescriptor(window, \"x\").value === 0;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A while ago, I created a JavaScript library named Xccessors, which implemented the legacy non-standard accessor (getter and setter) methods in IE8. I initially created two different libraries and decided that the library that implemented the legacy methods would be more useful at the time due to no changes needed in a JavaScript program\u2019s code [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[2,36,90,91,186],"class_list":["post-356","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-accessors","tag-ecmascript-5","tag-javascript","tag-javascript-libraries","tag-xccessors"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pfpUD-5K","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/posts\/356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/comments?post=356"}],"version-history":[{"count":0,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/posts\/356\/revisions"}],"wp:attachment":[{"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/media?parent=356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/categories?post=356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/tags?post=356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}