1 Star 0 Fork 0

癫疯丶歌谣/前端牛客网面试题刷题

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
手写bind220317.html 1.54 KB
一键复制 编辑 原始数据 按行查看 历史
yaojian01_ext 提交于 2022-04-08 20:04 . 修改
<!--
 * @Author: your name
 * @Date: 2022-03-15 14:10:54
* @LastEditTime: 2022-03-17 20:54:28
* @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \geyao\geyao\手写call220315.html
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>手写bind</title>
</head>
<body>
<script>
Function.prototype.mybind = function (target) {
//target:改变返回函数执行时的this指向
var obj = target || window;
console.log(arguments,"arguments")
var args = [].slice.call(arguments, 1); //获取bind时传入的绑定实参
var args1 = [].slice.call(arguments, 0); //获取bind时传入的绑定实参
console.log(args,"args")
console.log(args1,"args1")
var self = this; //要bind的函数
console.log(self,"self")
var _fn = function () {
var _args = [].slice.call(arguments, 0); //新函数执行时传递的实际参数
console.log(_args,"_args")
return self.apply(obj, args.concat(_args));
}
return _fn
}
var name = 'fangfang';
var obj = {
name: 'geyao'
};
function fn(a, b, c) {
console.log(a + b + c + this.name);
return a + b + c + this.name
};
fn.mybind(obj,"我的","老婆","")(); // 我的老婆是geyao
</script>
</body>
</html>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/geyaoisgeyao/geyao.git
git@gitee.com:geyaoisgeyao/geyao.git
geyaoisgeyao
geyao
前端牛客网面试题刷题
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385