Created
January 10, 2010 12:58
-
-
Save keeto/273490 to your computer and use it in GitHub Desktop.
An accessors mixin for getters and setters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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