js中call、apply、bind的区别
2024-09-01 19:23:50
var Person = {
name : 'alice',
say : function(txt1,txt2) {
console.info(txt1+txt2);
console.info(this.name);
}
} var Dog = {
name : 'tom',
say : function(txt1,txt2) {
console.info(txt1+txt2);
console.info(this.name);
}
}
var arr = ['hello','hi'];
Person.say('hello','hi');
Dog.say('wang~','wang2~');
Person.say.call(Dog,'hello','hi');//Person.say内部的this指向了Dog,多个参数用逗号隔开
Person.say.apply(Dog,arr);//第二个参数是数组,参数数量可以是未知的
var PersonSay = Person.say.bind(Dog,'hello','hi');//不会立即执行,触发返回函数才会执行
PersonSay(); >>>hellohi
>>>alice
>>>wang~wang2~
>>>tom
>>>hellohi
>>>tom
>>>hellohi
>>>tom
>>>hellohi
>>>tom
最新文章
- spark on yarn 提交任务出错
- react1
- HTML5 Canvas核心技术图形动画与游戏开发(读书笔记)----第一章,基础知识
- 提供给Android和iOS开发人员的UWP移植向导
- Red Hat dhclient
- 网上流行的add(2)(3)(4)
- DevExpress XtraReports 入门五 创建交叉表报表
- AppBarLayout学习笔记
- Mysql,zip格式安装、修改密码、建库
- LNMP环境的安装
- flask开发表单
- 最小公共祖先 (Tarjan) POJ1470
- 20190321xlVBA_汇总表按模板生成明细表
- [YARN] 2.2 GB of 2.1 GB virtual memory used. Killing container.
- maven 打 fat包(jar包有了全部依赖)插件
- 每天一个linux命令: /etc/group文件详解
- Linux服务器同步时间
- 【我的Android进阶之旅】Android 源代码中的Java代码中//$NON-NLS-1$ 注释是什么意思?
- .net core系列之《新一代的配置系统Configuration在支持多数据源,热更新,层级化方面代码快速实践》
- 如果plsql连接没问题,但程序中报ORA-12504的错误