JavaScript - 向回调函数中传递(绑定)this及其他参数
2020-01-15 07:20
781 查看
目录
主要参考
Function.prototype.bind()
语法:
[/code]fun.bind(thisArg[, arg1[, arg2[, ...])
描述:
创建一个与被调函数具有相同函数体的新函数(绑定函数)
新函数的 this 绑定到 bind() 的第一个参数 thisArg;- bind() 也接受将预设参数 arg1、arg2、... 提供给原函数
应用示例:配合 setTimeout
场景描述:
点击按钮,通过绑定的 this 对象控制按钮等待状态,并延迟 2s 后修改当前按钮字面值为新获取到的参数列表
代码示例:
[/code]var button = document.getElementById('BIND_FUNCTION'); button.addEventListener('tap', function() { mui(this).button('loading'); setTimeout(function() { mui(this).button('reset'); mui(this)[0].innerText = Array.prototype.slice.call(arguments); }.bind(this, 50, 100, 200), 2000); });
预设参数将插入到目标函数的参数列表开始位置
代码示例:
[/code]function list() { return Array.prototype.slice.call(arguments); } var list1 = list(1, 2, 3); // [1, 2, 3] // 创建一个list的绑定函数newList,并插入一个预设参数37 var newList = list.bind(undefined, 37); var list2 = newList(1, 2, 3); // [37, 1, 2, 3]
转载于:https://www.cnblogs.com/JustBeZero/p/9407573.html
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- JavaScript---事件绑定方式(传递参数)
- 使用jQuery绑定事件,为回调函数传递参数遇到的坑
- <a>标签中 href 和 onclick 的区别,以及href="javascript:xxx(this);"与onclick="xxx(this);" 传递this参数的区别
- javascript 指定绑定函数名称,并且传递参数
- Javascript attachEvent传递参数的办法
- JavaScript实现的base62 encode/decode,用于页面上直接对作为参数传递的url本身的编码和解码。
- JavaScript(js) 前台 参数传递乱码 转码 问题--已解决! 中文字符乱码
- JQuery 绑定事件时传递参数的实现方法
- Springmvc 前台传递参数到后台需要数据绑定
- (4)事件处理——(6)给.ready()回调函数传递一个参数(Passing an argument to the .ready() callback)
- javascript传递参数给cgi
- Windows Phone 7 MVVM模式数据绑定和传递参数
- 关于javascript URL 传递参数乱码的问题及解决方案。
- Javascript attachEvent传递参数的办法
- Windows Phone 7 MVVM模式数据绑定和传递参数 from:http://www.cnblogs.com/linzheng/archive/2011/03/27/1997086.html
- 通过其他类为主类传递参数
- javascript+attachEvent传递参数问题
- Javascript 获取请求路径中传递的参数
- javascript让setInteval里的函数参数中的this指向特定的对象
- javascript实现浏览器窗口传递参数的方法