call、apply、bind的区别和自定义
SegmentFault
共 2295字,需浏览 5分钟
·
2020-09-04 13:19
来源:SegmentFault 思否社区
作者:黄万通
区别call()/apply()/bind()
call(obj)/apply(obj): 调用函数, 指定函数中的this为第一个参数的值 bind(obj): 返回一个新的函数, 新函数内部会调用原来的函数, 且this为bind()指定的第一参数的值 注意: 如果obj是null/undefined, this为window
应用
call()/bind()应用: 根据伪数组生成真数组
bind(): react中组件的自定义方法 / vue中的事件回调函数内部自定义call()/apply()
1). 给obj添加一个临时方法, 方法名任意, 值为当前函数
2). 通过obj调用这个临时方法, 并将接收的参数传入
3). 删除obj上的这个临时方法属性自定义实现bind()
1). 返回一个新函数
2). 在新函数内部通过原函数对象的call方法来执行原函数, 指定原函数的this为obj指定参数为bind调用的参数和后面新函数调用的参数
自定义call()/apply()/bind()
自定义call()/apply()
1). 给obj添加一个临时方法, 方法名任意, 值为当前函数
2). 通过obj调用这个临时方法, 并将接收的参数传入
3). 删除obj上的这个临时方法属性
4). 代码
Function.prototype.call = function (obj, ...args) {
// this() 就是调用当前函数实例
// 处理obj是undefined或者null的情况
if (obj===undefined || obj===null) {
obj = window
}
// 给obj添加一个方法: tempFn: this
obj.tempFn = this
// 调用obj的tempFn方法, 传入rags参数, 得到返回值
const result = obj.tempFn(...args)
// 删除obj上的temFn
delete obj.tempFn
// 返回方法的返回值
return result
}
Function.prototype.apply = function (obj, args) {
// 处理obj是undefined或者null的情况
if (obj===undefined || obj===null) {
obj = window
}
// 给obj添加一个方法: tempFn: this
obj.tempFn = this
// 调用obj的tempFn方法, 传入rags参数, 得到返回值
const result = obj.tempFn(...args)
// 删除obj上的temFn
delete obj.tempFn
// 返回方法的返回值
return result
}
自定义实现bind()
1). 返回一个新函数
2). 在新函数内部通过原函数对象的call方法来执行原函数指定this为obj指定参数为bind调用的参数和后面新函数调用的参数
3).代码
Function.prototype.bind = function (obj, ...args) {
// 返回一个新函数
return (...args2) => {
// 调用原来函数, 指定this为obj, 参数列表由args和args2依次组成
return this.call(obj, ...args, ...args2)
}
}
测试
function fn(x, y) {
this.aaa = 3 // 会给改变this对象添加aaa属性的值为3,如果指定的对象为null会给window添加
console.log(x, y, this, arguments.length)
return x + y
}
const obj = {m: 0}
fn(10, 20) // 直接调用会在window全局对象下添加aaa属性
fn.call(obj, 10, 20) // 相当于obj.fn(10, 20) 打印: 10 20 obj 2
fn.apply(obj, [1, 2]) // 相当于obj.fn(1, 2) 打印: 1 2 obj 2
fn.call(null, 1, 2) // 和直接调用一样 打印: 1 2 Window 2
fn.bind(obj)(3, 4) // 打印: 3, 4 obj 2
fn.bind(obj,10)(3, 4) // 打印: 10, 3 obj 3
评论
crm和scrm的区别
SCRM全称:social crm,社会化客户关系管理; CRM之父”的Paul Greenberg写完他的CRM经典著作第三版之后,他自己也不得不食言重新写作第四版。原因很简单,范式的改变,让Social CRM(以下简称SCRM)走上了舞台。
传统CRM客户关系管理是一种通过系统和技术手段实现的服务和商业策略,目的是提高客户在与企业交互时的体验。那么,是什么催化了CRM的社会化趋势呢?随着社会化媒体的诞生、发展,越来越多的消费者聚集在社会化媒体中,企业品牌的客户管理也随之发生了改变。
CRM
传统CRM的重点是收集和管理静态的客户资料,如过去的购买信息,联系记录和客户属性信息。传统CRM是企业主导的,属于内向型的,企业注重
启服云知识产权管理系统
0
管理和人力资源的区别?
特别好的问题!想得非常深了!和你交流一下我目前的看法。
1. 管理的定义很多,但是核心都是“有意识地协调两个及以上人行为的活动”,所以管理的本质是整合、协调他人来完成共同目标,我老师喜欢概括为“用人办事”。
2. 人力资源管理是一项专门的职能,与管理相同的是:都强调如何使用人力资源。
3. 人力资源管理与管理的差别在于,前者更强调如何对人力资源进行价值评价和价值回报,后者更强调人力资源的使用,强调共同目标的达成。
蒋诚潇
0
SCRM和CRM的区别
1.可以触达客户(随时互动,加深印象);
2.可以做到客户的全生命周期(LT)的管理;
3.企业微信是承接客户的最佳载体,和客户建立社交关系;
4.丰富的标签能力,精准的客户画像,是良好客户关系的前提,传统CRM信息缺失;
5.将客户在线化,形成数字化客户资产,可以智能统计户关系管理千人千面”
17.SCRM,也可以称为营销CRM;
18.更加以客户为中心;
19. SCRM的S,可以有四层含义:Social社交,Stack全栈,Smart智能,Simple简单。和分析客情;
6.数字化,可以精准洞察客户需求,销售可以精准捕捉商机;
7.丰富的功能模块,可以提升销售的工作效率,提升转化效率。
8.无需多平台切换,
非著名专家
0