Skip to content

Instantly share code, notes, and snippets.

@mg
Created December 22, 2014 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mg/c0d1effa4b89b264f607 to your computer and use it in GitHub Desktop.
Save mg/c0d1effa4b89b264f607 to your computer and use it in GitHub Desktop.
function prop(obj, name, get, set) {
var base= name.substr(0,1).toUpperCase() + name.substr(1);
if(!get) {
get= function() {
return this['_' + name];
};
}
if(!set) {
set= function(v) {
this['_' + name]= v;
return this;
};
}
obj.prototype[name]= function(v) {
if(v === undefined) {
return this['get' + base]();
}
return this['set' + base](v);
};
obj.prototype['get' + base]= get;
obj.prototype['set' + base]= set;
}
function emptyFunc() {
}
module.exports= function(obj) {
if(obj === undefined) obj= function() {};
var me= {
Prop: function(name, get, set) {
prop(obj, name, get, set);
return me;
},
GetProp: function(name, get) {
prop(obj, name, get, emptyFunc);
return me;
},
SetProp: function(name, set) {
prop(obj, name, emptyFunc, set);
return me;
},
Constructor: function() {
return function() {
if(false === (this instanceof obj)) {
return new obj();
}
return obj();
};
},
Prototype: obj.prototype
};
return me;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment