我让关闭工作,但我想做的是如果我可以摆脱括号:
window.obj = {}
obj.method = (function() {
var closure
return function(value) {
if (arguments.length) {
closure = value
} else {
return closure
}
}
})();
obj.method(3.14)
$('body').append(obj.method())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
问:有没有办法摆脱领先(和尾随)()?
答案 0 :(得分:0)
window.obj = {}
obj.init = function() {
var closure
return result
function result(value) {
if (arguments.length) {
closure = value
} else {
return closure
}
}
}
obj.method = obj.init()
obj.method(Math.PI)
document.getElementById('output').innerHTML = obj.method()
&#13;
<div id="output"></div>
&#13;