首页  >  问答  >  正文

javascript - 想请教一下,js中 function中参数 e 到底是什么,每个条用的参数 e的用法都不一样?

#

某草草某草草2672 天前1878

全部回复(11)我来回复

  • 伊谢尔伦

    伊谢尔伦2017-06-26 10:59:08

    我估计是回调函数没理解好,
    举个例子:

    //Array.prototype.sort的一种用法是传进一个函数判断大小返回布尔值
    var arr = [5,2,4,3]
    
    var sortFunc = function(a, b) {
        return a - b
    }
    
    console.log(arr.sort(sortFunc)) //排序好了
    
    //那sortFunc里面的a,b从哪里来的?
    //在Array.prototype.sort里面有具体实现,
    //糟糕,写不出冒泡排序...
    Array.prototype.sort = function(fn) {
        for(var i=0; i<this.length; i++) {
            for(var j=i+1; j<this.length;j++) {
                //很难受
                var bool = fn(this[i], this[j]) > 0
                if(bool) {
                    //换
                }
            }
        }
    }
    //重点在于fn是Array.prototype.sort的形参,实参是sortFunc,
    //sortFunc接收俩参数,这是Array.prototype.sort决定的,因为其调用
    var bool = fn(this[i], this[j]) > 0//这句话就是调用了sortFunc,
    //this[i,j]分别对应sortFunc形参的ab,

    以上是蒙的sort代码,错了大家可以喷我。

    回复
    0
  • 取消回复