Skip to content

Instantly share code, notes, and snippets.

@dachi023
Last active September 10, 2015 17:12
Show Gist options
  • Save dachi023/79fdb117363845d4c36b to your computer and use it in GitHub Desktop.
Save dachi023/79fdb117363845d4c36b to your computer and use it in GitHub Desktop.
JavaScriptでgetter-setter
#Add getter-setter
_prop = (initValue) ->
do (val = initValue) ->
_val = val
(val) ->
return _val if val is undefined
_val = val
#Example
myValue = _prop 'hello'
myValue() #=> hello
myValue 'world'
myValue() #=> world
###JavaScript
//Add getter-setter
var _prop, myValue;
_prop = function(initValue) {
return (function(val) {
var _val;
_val = val;
return function(val) {
if (val === void 0) {
return _val;
}
return _val = val;
};
})(initValue);
};
//Example
myValue = _prop('hello');
myValue(); //=> hello
myValue('world');
myValue(); //=> world
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment