call、apply、bind的区别(简洁)

call、apply、bind的区别,欢迎指正。

console.log('---------------------call/apply/bind区别---------------------')

// call/apply/bind区别

let Fcall = function(name) {
    this.name = name
    this.say = function() {
        console.log(this.name)
    }
    this.say()
    
}
var fcall = {}

Fcall.call(fcall,'che-call') // 参数一个传,立即执行 che-call
Fcall.apply(fcall,['che-apply']) // 参数数组传,立即执行 che-apply
var bin = Fcall.bind(fcall,'che-bind') // bind不会立即执行,会返回函数 undefined
bin() // 返回后的函数需要手动执行 che-bind

// 应用
let array = [1, 7, 5]
let max = Math.max.apply(null, array); // 取数组最大值 7
let min = Math.min.apply(null, array); // 取数组最小值 1
let max1 = Math.max(...array) // es6+简洁版求数组最大值、跟本知识无关 7
let min1 = Math.min(...array) // es6+简洁版求数组最小值、跟本知识无关 1
console.log(max , min, max1, min1) // 7 1 7 1

let arr1 = [1, 2];
let arr2 = [4, 5];
Array.prototype.push.apply(arr1, arr2); // 数组合并,但是改变了arr1,不推荐
let arres6 = [...arr1,...arr2] // es6简洁版合并, 不会改变原数组
console.log(arr1, arres6); // arr1 [1, 2, 4, 5] arres6 [1, 2, 4, 5, 4, 5]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值