js中函数的调用形式

2020-02-10  本文已影响0人  五四青年_4e7d

1.普通函数:

function info(a,b){
   return a + b
}
console.log(info(9,8))

2.对象的方法:

var sduntent = {
    min:3,
    max:89,
    show:function(min,max){
      return this.min+this.max
    }
}
console.log(sduntent.show())

3.事件处理函数:

$("#but").on("click",function(){
    console.log(this)
})

4.回调函数:

//执行callback 并且把callback 拿过来
var a = [1,4,5,6,77,8,9,53,32,112]
var b = [232,4,522,6,77,8,9,53,32,112]
function each(arr,callback){
    for(var i = 0; i < arr.length;i++){
        var item = arr[i]
        callback(item)
    }
}
each(a,function(item){
    if(item>100){
        console.log(item)
    }
})
//简单的写法
each(b,master)
function master(item){
    if(item > 200){
        console.log(item)
    }
}
上一篇 下一篇

猜你喜欢

热点阅读