js一些常用方法的掌握

js一些常用方法掌握,箭头函数(python中的lambda函数)初体验

<script>
    // forEach()
    function run1() {
        var x = [1, 2, 3, 4, 5, 7, 8, 9];
        x.forEach(function(each) {
            console.log(each);
        });
    }
    // map()
    function run2() {
        let arr = [1, 2, 3, 4, 5];
        const arr_res = arr.map(y => y * 2);
        // const arr_res = arr.map(x => x * 2);
        console.log(arr_res);
    }
    // for each in all                like   python that method!
    function run3() {
        let arr = [1, 2, 3, 4, 5];
        for (each in arr) {
            console.log(each);
        }
    }
    // every   some
    function run4() {
        // every
        var arr = [1, 6, 8, -2, -5, 7, -4]
        var isPositive = arr.every(function(value) {
            return value > 0;
        })
        console.log(isPositive) // false
            //some
        var arr = [1, 6, 8, -2, -5, 7, -4]
        var isPositive = arr.some(function(value) {
            return value > 0;
        })
        console.log(isPositive) // true
    }
    // do not speak it !  function reduce is so awesome!        error word:aweasome!
    function run5() {
        var arr = [1, 2, 3, 4, 5, 6]
        console.log(arr.reduce(function(v1, v2) {
                return v1 + v2;
            })) // 21
            //开始是1+2 = 3,之后3+3 =6,之后6+4 =10,之后
            //10+5 =15,最后15+6 =21
        console.log(arr.reduce(function(v1, v2) {
                return v1 - v2;
            }, 100)) // 79
            //开始的时候100-1=99,之后99-2=97,之后97-3=94,之后
            //94-4=90,之后90-5=85,最后85-6=79
    }
    // lambda   而在es6的箭头函数出现后,lambda表达式就成了开发时的必备技能了
    // 当然最主要的原因也是因为这个方便了我们使用一些回调函数
    //例如
    // [1,2,3].reduce((a,b)=>a+b)//6
    // [1,2,3].map(a=>a*2)//2,4,6
    // ...
    function run6() {
        //例如
        let max = (a, b) => a > b ? a : b;
        //等于
        let max = function(a, b) {
            return a > b ? a : b;
        }


    }

    function run7() {}

    function run8() {}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值