函数的定义和调用

函数的定义

1、函数声明

    function fn_1() {
        console.log('函数声明')
    }

2、函数表达式

    var fn_2 = function () {
        console.log('函数表达式')
    }

3、构造函数

var fn_3 = new Function('a', 'b', 'return a+b')

函数的调用

1、函数名

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

2、自调用

    (function (a) {
         console.log(a)
         console.log('匿名函数')
     })(1)


   var fn_2 = function () {
        console.log('函数表达式')  //函数表达式
    }()



     (function fn_1() {
         console.log('函数声明')
     })()

3、call() apply()

   function fn_1() {
        console.log('函数声明')  //函数声明
    }
    fn_1.call()

4、在对象中调用函数

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

5、在数组中调用函数

   var arr = [1, true, null, { name: 'name' }, function () { console.log(this) }]  //[1,true,null,{...},f]
    arr[4]()

6、函数作为参数

 function fn_4(x) {
        // console.log(x)
        return (x)
    }
    // fn_4()
    // fn_4(true)
    fn_4(function () { console.log('是函数也是参数') })

7、函数作为返回值

   function fn_5(x) {
        return x
    }
    console.log(fn_5(function () { console.log('是函数也是返回值') }))  //f(){console.log('是函数也是返回值')}
    var reusit = fn_5(function () { console.log('是函数也是返回值') }) //是函数也是返回值
    reusit()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值