Last active
September 10, 2015 17:12
-
-
Save dachi023/79fdb117363845d4c36b to your computer and use it in GitHub Desktop.
JavaScriptでgetter-setter
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
#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