函数调用的七种方式

函数的调用

函数名()

function fn_1() {
        console.log('函数声明');
    }
    var fn_2 = function () {
        console.log('函数表达式');
    }
    fn_1()
    fn_2()
    //构造函数
    var fn_3 = new Function('a', 'b', 'return a+b');
    console.log(fn_3(1, 2));

匿名函数自调用

 (function () {
        console.log('函数自调用');
    })()

call()、apply()

 function fn_4() {
        console.log('call()调用');
    }
    fn_4.call()

在对象中调用函数

 var obj = {
        name: 'name',
        play: function () {
            console.log(name);
            console.log(this.name);
        }
    }
    obj.play()

在数组中调用函数

var arr = [0, 1, 2, function () { console.log('函数中调用'); }]
    arr[3]()

函数作为参数

function fn_5(x) {
        console.log(x);
        x()
    }
    fn_5(function () { console.log('函数作为参数') });

函数作为返回值

 function fn_6(x) {
        return x;
    }
    var returnvalue = fn_6(function () { console.log('函数作为返回值'); })
    returnvalue()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值