试图摆脱自我执行的括号

时间:2016-05-19 17:18:42

标签: javascript

我让关闭工作,但我想做的是如果我可以摆脱括号:

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>

问:有没有办法摆脱领先(和尾随)()?

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
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;
&#13;
&#13;