Skip to content

Instantly share code, notes, and snippets.

@keeto
Created January 10, 2010 12:58
Show Gist options
  • Save keeto/273490 to your computer and use it in GitHub Desktop.
Save keeto/273490 to your computer and use it in GitHub Desktop.
An accessors mixin for getters and setters.
/*
Script: Accessors.js
An accessors mixin for getters and setters.
License & Copyright:
Copyright 2009, Mark Obcena <keetology.com>
MIT-Style License
*/
var Accessors = new Class({
accessors: {},
defineGetter: function(key, fn){
this.accessors[key] = this['get' + key.capitalize()] = fn;
return this;
},
defineSetter: function(key, fn){
this.accessors[key] = this['set' + key.capitalize()] = fn;
return this;
},
get: function(key){
if (this.accessors[key]) return this.accessors[key].call(this);
return this;
},
set: function(key, value){
if (this.accessors[key]) return this.accessors[key].call(this, value);
return this;
},
removeGetter: function(key){
delete this.accessors[key];
delete this['get' + key.capitalize];
return this;
},
removeSetter: function(key){
delete this.accessors[key];
delete this['set' + key.capitalize];
return this;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment