箭头函数、this指向 call()apply()bind()

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<body>

    <div id="con">ddd</div>

</body>

<script>

    // function() { }

    // es6 箭头函数

    // 1.把function 去掉

    // 2.在()与{ } 添加箭头

    // () => { }

    // function (item, i) { }

    // (item, i) => { }

    // function() { return "hello"; }

    // () => { return "hello"; }

    // 若有返回值  {} 与  return 还可以省略

    // () => "hello"

    // 若参数只有一个 () 还可省略

    // function(item) { return item }

    // item => item


 

    // function() { return a + b };

    // () => { return a + b }

    // () => a + b;


 

    // function(a, b) { return a + b };

    // (a, b) => { return a + b; }

    // (a, b) => a + b;

    // function(a) { return a };

    // (a) => a;

    // a => a;


 

    // arr.forEach(function (item, i, arr) { console.log(item); })

    // arr.forEach((item, i, arr) => { console.log(item); })


 

    // arr.filter(function (item, i) { return arr.indexOf(item) === i })

    // arr.filter((item, i) => arr.indexOf(item) === i)

    // arr.map(function (item) { return item; })

    // arr.map(item => item)


 

    // function box() { }

    // var box = () => { console.log(this); }

    // var box = function(){}

    // box();


 

    var oDiv = document.getElementById("con");

    function box(a, b) {

        console.log(this);//window    oDiv

        // this.innerHTML = "HELLO WROLD";

        return () => this;

        // return function () {

        //     return this;

        // };

    }


 

    // console.log(box.call(oDiv)());  //box()()   闭包里的this 指向window对象

    // console.log(box.call(oDiv, null)());

    // console.log(box());//window

    // window.box(2,6)

    // box.call(oDiv, 2, 6);//强制修改函数box内部的this指向

    // box.apply(oDiv, [2, 6]);//强制修改函数box内部的this指向

    // box.bind(oDiv)  // box

    // box.bind(oDiv, [2, 6])()  // box()

    // console.log();


 

    // 函数名.call(参数1,参数2)

    // 参数1:函数内部的this强制指向的对象

    // 参数2:函数的实参  多个实参间用逗号隔开

    // 函数名.apply(参数1,参数2)

    // 参数1:函数内部的this强制指向的对象

    // 参数2:数组  函数的实参组成的数组

    // 函数名.bind(参数1,参数2)

    // 参数1:函数内部的this强制指向的对象

    // 参数2:数组  函数的实参组成的数组

    // 返回: 函数

    // 调用函数

    // 函数名.bind(参数1,参数2)()

    // console.log(box()());//window



 

    // function () {

    //     return this;

    // };


 

    // function Box() {

    // }

    // var Box = () => { }

</script>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值