Cross-browser Getters/Setters (including IE)


/ Published in: JavaScript
Save to your folder(s)

Xccessors is a JavaScript shim that implements the legacy methods for defining and looking up object accessors (getters and setters) of objects in JavaScript using ECMAScript 3.1’s accessors standard. This is aimed at adding support for the legacy method in IE8 RC1.

The code is made to have as small of a footprint as possible. YUI minified, the code is 556 bytes. Here is the minified version:

(function(b,d,c){function a(f,g,e){if(f in d&&!(g in {})){d[c][g]=Element[c][g]=Window[c][g]=HTMLDocument[c][g]=e}}a(b[0],b[2],function(f,e){d[b[0]](this,f,{get:e})});a(b[0],b[3],function(f,e){d[b[0]](this,f,{set:e})});a(b[1],b[4],function(e){return d[b[1]](this,e).get||d[b[1]](this.constructor.prototype,e).get});a(b[1],b[5],function(e){return d[b[1]](this,e).set||d[b[1]](this.constructor.prototype,e).set})})(["defineProperty","getOwnPropertyDescriptor","__defineGetter__","__defineSetter__","__lookupGetter__","__lookupSetter__"],Object,"prototype");


Copy this code and paste it in your HTML
  1. /*
  2. @name Xccessors
  3. @version 0.0.3
  4. @desc Shim that implements __defineGetter__, __defineSetter__, __lookupGetter__, and __lookupSetter__ in browsers that have ECMAScript 3.1 accessor support but not the legacy methods
  5. @license http://www.gnu.org/licenses/lgpl.html
  6. @author Elijah Grey - eligrey.com
  7. */
  8. (function (methods, o, f) {
  9. function extendMethod(reqMethod, method, fun) {
  10. if (reqMethod in o && !(method in {})) o[f][method] = Element[f][method] = Window[f][method] = HTMLDocument[f][method] = fun;
  11. };
  12. extendMethod(methods[0], methods[2], function (prop, fun) {
  13. o[methods[0]](this, prop, { get: fun });
  14. });
  15. extendMethod(methods[0], methods[3], function (prop, fun) {
  16. o[methods[0]](this, prop, { set: fun });
  17. });
  18. extendMethod(methods[1], methods[4], function (prop) {
  19. return o[methods[1]](this, prop).get
  20. || o[methods[1]](this.constructor.prototype, prop).get;
  21. });
  22. extendMethod(methods[1], methods[5], function (prop) {
  23. return o[methods[1]](this, prop).set
  24. || o[methods[1]](this.constructor.prototype, prop).set;
  25. });
  26. })(["defineProperty", "getOwnPropertyDescriptor","__defineGetter__","__defineSetter__","__lookupGetter__","__lookupSetter__"], Object, "prototype");

URL: http://eligrey.com/projects/xccessors/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.