Skip to content

Instantly share code, notes, and snippets.

@Witiko
Created September 6, 2013 10:47
Show Gist options
  • Save Witiko/6462265 to your computer and use it in GitHub Desktop.
Save Witiko/6462265 to your computer and use it in GitHub Desktop.
An interfacing library for the ecma5 or mozilla getter and setter support - whichever is implemented
var Accessors = (function($, undefined) {
var ECMA5Support =
Object.getOwnPropertyDescriptor instanceof Function &&
Object.defineProperty instanceof Function && (function() {
var o = {}, f = function() {return true};
try {
Object.defineProperty(o, "o", {get: f});
return o.o && Object.getOwnPropertyDescriptor(o, "o").get === f;
} catch(e) {
return false;
}
}
)(), MozillaModelSupport =
Object.prototype.__defineGetter__ instanceof Function &&
Object.prototype.__defineSetter__ instanceof Function &&
Object.prototype.__lookupGetter__ instanceof Function &&
Object.prototype.__lookupSetter__ instanceof Function;
if(ECMA5Support || MozillaModelSupport) {
if(MozillaModelSupport) {
$.append = function(object, property, opts) {
if(opts.get instanceof Function) $.getter(object, property, opts.get);
if(opts.set instanceof Function) $.setter(object, property, opts.set);
};
$.getter = function(object, property, func) {
if(func instanceof Function)
object.__defineGetter__(property, func);
else return object.__lookupGetter__(property);
};
$.setter = function(object, property, func) {
if(func instanceof Function)
object.__defineSetter__(property, func);
else return object.__lookupSetter__(property);
};
$.retrieve = function(object, property) {
var o = {};
o.get = object.__lookupGetter__(property);
o.set = object.__lookupSetter__(property);
return o;
};
} else {
$.append = function(object, property, opts) {
var desc = object.hasOwnProperty(property)?
Object.getOwnPropertyDescriptor(object, property):{
configurable: true,
enumerable: true
};
if(opts.get instanceof Function) desc.get = opts.get;
if(opts.set instanceof Function) desc.set = opts.set;
delete object[property];
Object.defineProperty(object, property, desc);
};
$.getter = function(object, property, func) {
var exists = object.hasOwnProperty(property),
setting = func instanceof Function;
if(!exists && !setting) return;
var desc = exists?
Object.getOwnPropertyDescriptor(object, property):{
configurable: true,
enumerable: true
};
if(setting) {
desc.get = func;
delete object[property];
Object.defineProperty(object, property, desc);
} else return desc.get;
};
$.setter = function(object, property, func) {
var exists = object.hasOwnProperty(property),
setting = func instanceof Function;
if(!exists && !setting) return;
var desc = exists?
Object.getOwnPropertyDescriptor(object, property):{
configurable: true,
enumerable: true
};
if(setting) {
desc.set = func;
delete object[property];
Object.defineProperty(object, property, desc);
} else return desc.set;
};
$.retrieve = function(object, property) {
var desc = Object.getOwnPropertyDescriptor(object, property), o = {};
o.get = desc.get;
o.set = desc.set;
return o;
};
} $.supported = true;
} else $.supported = false;
if(Object.freeze instanceof Function)
Object.freeze($);
return $;
})({});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment