Created
December 22, 2014 10:45
-
-
Save mg/c0d1effa4b89b264f607 to your computer and use it in GitHub Desktop.
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
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