// Create an method of all object
            
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};

/* * * * * * * * * * * * * * * * * * * * *
 *  为什么this is [4, 8, 15, 16, 23, 42]  *
 * * * * * * * * * * * * * * * * * * * * */
Array.method('reduce', function(f, value){ 
for (i = 0; i < this.length; i += 1) {
                    value = f(this[i], value);
                }
                return value;
            });
            
            // Create an array of numbers.
            
            var data = [4, 8, 15, 16, 23, 42];
            
            // Define two simple functions. One will add two
            // numbers. The other will multiply two numbers.
            
            var add = function(a, b){
                return a + b;
            };
            // Invoke the data's reduce method, passing in the
            // add function.
            
            var sum = data.reduce(add, 0); // sum is 108

解决方案 »

  1.   


     Function.prototype.method = function (name, func) {
                    this.prototype[name] = func; //这里的this,对于下面的array引用的就是array,也就是下面传进来的[4, 8, 15, 16, 23, 42]
                    return this;
                };
      

  2.   

    因为 reduce 是 Array 滴实例方法,而 data 滴类型是 Array,
    而实例方法中 this 就是对对象自身滴引用,因此当然就是 [4, 8, 15, 16, 23, 42]!
      

  3.   


     // 其实主要是这句
    Function.prototype.method = function (name, func) {
          this.prototype[name] = func;
          return this;
    };
    这个将所有Object的原型绑定了method方法.当然Array也不例外.
    所以当Array.method('reduce', function(){...})就调用了上面的绑定执行了里面的方法
    就相当于Array.prototype.reduce = function(){
                    for (i = 0; i < this.length; i += 1) {
                        value = f(this[i], value);
                    }
                    return value;
    }
    所以this当然就指向了Array
      

  4.   


    // 我这有版连续求值<script type="text/javascript">
    <!--
        Function.prototype.$continuous = function(fn) {
            var me = this;
            return function(){
                var currentArgs = Array.prototype.slice.call(arguments, 0, me.length);
                var moreArgs = Array.prototype.slice.call(arguments, me.length);
                ret = me.apply(this, currentArgs);
                if(moreArgs.length > 0) {
                    ret = fn.call(this, arguments.callee, ret, moreArgs);
                }
                return ret;
            }
        }
        
        function add(x, y){
            return x + y;
        }    function max(x, y) {
            return x > y ? x : y;
        }    // 先链接返回值和参数然后返回给函数递归调用
        function reducer(target, returnValue, moreArgs) {
            return target.apply(this, [returnValue].concat(moreArgs));
        }
        // 先进行递归调用 然后将返回值装载到数组
        function processer(target, returnValue, moreArgs) {
            return [returnValue].concat(target.apply(this, moreArgs));
        }    
        // 连续加法运算
        add = add.$continuous(reducer);
        var a = add(1,2,3,4,5);
        alert(a)
        // 连续求最大值
        max = max.$continuous(reducer);
        var m = max(1,3,42,12,3);
        alert(m)
        // 返回元素数组
        var $ = function(id) {
            return document.getElementById(id);
        }.$continuous(processer);    var els = $("a", "b", "c");
        alert(els)
    //-->
    </script>