javascript

2018-05-03 call apply bind

2018-05-03  本文已影响0人  wuliduoduo
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        var sum1=Math.max(1,2,3,4,5);
        console.log(sum1);
        var sum2=Math.max.apply(null,[1,2,3,4,5]) //null代表this
        console.log(sum2);
        var sum3=Math.max.call(null,1,2,3,4,5) //apply数组,call参数
        console.log(sum3);
        var sum4=Math.max.bind(null,1,2,3,4,5) //bind相当默认参数
        console.log(sum4());
        console.log(sum4(7,8));

        function add(x,y){
            return x+y;
        }

        var plus1=add.bind(null,1);
        console.log(plus1(5));

        //等价于
        var plus2=function plus1(y){
            return add(1,y)
        }
        console.log(plus2(5))
    </script>
</body>
</html>
上一篇下一篇

猜你喜欢

热点阅读