Created
July 23, 2014 14:58
-
-
Save jikeytang/56b2980db7237ac6a8ce to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140724-题目1
This file contains hidden or 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
// 感谢[广州—坚壳]贡献 | |
实现一个方法,使其达到如下的输出结果,比如: | |
fn(2); ==> 2; | |
fn(2)(3); ==> 6; | |
fn(2)(3)(3); ==> 18; | |
PS: | |
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。 | |
```javascript | |
// you code | |
``` | |
2. 粘贴代码时请使用shift+tab,缩进前面的空白。 |
bairedzhang
commented
Jul 23, 2014
//方法一,利用function是对象,对象可以设置自定义属性的特点
function fn(n){
!fn.first && (fn.re = 1,fn.first = true)
fn.re *= n;
fn.toString = function(){
return fn.re;
};
return fn;
}
console.log(fn(2)(3))
//方法二:闭包
var fn = (function(){
var r = 1,
f = function(n){
r *= n;
return f;
}
f.toString = function(){
return r;
}
return f;
})();
console.log(fn(2)(3))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment