关于reduce,call,apply函数的记录

Posted on 2020-03-25 09:56  昭冥大人呀  阅读(553)  评论(0)    收藏  举报
//1, 关于reduce函数,我的理解是迭代运算,通常可以用作累加求和
            var arr = [1,2,3,4]
            console.log(arr.reduce(function(a,b){ return a + b})) // 1+2+3+4 = 45  
            console.log(arr.reduce(function(a,b){ return (a + b)*2})) //(((1+2)*2 +3)*2 + 4)*2=44
            console.log(arr.reduce(function(a,b){ return a * b})) // 1*2*3*4=24
            
            //2, 函数重用 all,call传入的是参数列表
            var person = {
              fullName: function(city, country) {
                return this.firstName + " " + this.lastName + "," + city + "," + country;
              }
            }
            var person1 = {
              firstName:"John",
              lastName: "Doe"
            }
            console.log(person.fullName.call(person1, "Oslo", "Norway"));
            console.log(Math.max(1,2,3)) // 3
            console.log(Math.max.call('',1,2,3,4)) // 4
            
            //3, 函数重用 all,call传入的是参数列表
            console.log(Math.max(1,2,3)) // 3
            console.log(Math.max.apply('', [1,2,3,4])) // 4
            var person = {
              fullName: function(city, country) {
                return this.firstName + " " + this.lastName + "," + city + "," + country;
              }
            }
            var person1 = {
              firstName:"John",
              lastName: "Doe"
            }
            console.log(person.fullName.apply(person1, ["Oslo", "Norway"]));