如何将函数名作为参数传递与将函数调用包含在匿名函数中并将它们作为整体传递不同

时间:2017-05-15 12:50:43

标签: javascript callback

如何将函数名称作为参数传递,而不是将函数调用包含在匿名函数中并将它们作为整体传递。 为什么即使我将函数调用为相同的顺序,两个输出也是不同的。

案例1



class BaseCell: UICollectionViewCell {

override init(frame: CGRect){
    super.init(frame: frame)
    setupViews()
}

required init?(coder aDecoder: NSCoder){
    fatalError("init(coder:)has not been implemented")
}

func setupViews(){
    backgroundColor = UIColor.white
}
}




案例2



function one(cb) {
    console.log("one");
    setTimeout(cb, 1000);
}

function two(cb) {
    console.log("two");
    setTimeout(cb, 1000);
}

function three(){
    console.log("three");
}

one(function(){two(three)});




这两行在程序执行方面有何不同

function one(cb) {
    console.log("one");
    setTimeout(cb, 1000);
}

function two(cb) {
    console.log("two");
    setTimeout(cb, 1000);
}

function three(){
    console.log("three");
}

one(two(three));

1 个答案:

答案 0 :(得分:1)

发生这种情况是因为两个(...)与两个

不一样

案例1: 你用函数作为参数调用一个。

案例2: 你用一个两(三)作为参数调用一个。这不再是一个功能。 就像function(x){return x*x}(5)等于25并且不等于函数函数。