码迷,mamicode.com
首页 > 其他好文 > 详细

arguments.callee的用法

时间:2016-09-07 22:44:01      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

1.今天在看高阶函数,其实currying的一个函数中,有那个arguments.callee,表示不见过,查了查。

arguments.callee 在哪一个函数中运行,它就代表哪个函数。 一般用在匿名函数中。

在匿名函数中有时会需要自己调用自己,但是由于是匿名函数,没有名子,无名可调。

这时就可以用arguments.callee来代替匿名的函数

  

var currying = function ( fn ) {
       var args = [];

       return function () {
           if( arguments.length ===0 ){
               return fn.apply( this, args);
           }else{
               [].push.apply(args, arguments);
               return arguments.callee;
           }
       }
   };
    
    var cost = (function () {
        var money = 0;
        
        return function () {
            for( var i = 0,l = arguments.length; i < l; i++){
                money += arguments[i];
            }
            return money;
        }
    })()

    var cost = currying( cost );
    
    cost(100);
    cost(200);
    cost(300);
    
    alert(cost()) //600

  

arguments.callee的用法

标签:

原文地址:http://www.cnblogs.com/chenjinxinlove/p/5851073.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!