Home  >  Q&A  >  body text

javascript - I would like to ask, what is the parameter e in function in js? Is the usage of parameter e different in each article?

某草草某草草2672 days ago1879

reply all(11)I'll reply

  • 伊谢尔伦

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

    I guess I didn’t understand the callback function well,
    For example:

    //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,

    The above is Meng’s sort code. If I’m wrong, you can blame me.

    reply
    0
  • Cancelreply