1 Star 0 Fork 0

Shadow / code-snippet

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
call_apply_bind.html 656 Bytes
一键复制 编辑 原始数据 按行查看 历史
Shadow 提交于 2022-04-20 22:48 . 手写call、apply、bind
<script>
function call(obj, fn, ...args) {
obj = obj || window;
const symbol = Symbol();
obj[symbol] = fn;
obj[symbol](...args);
delete obj[symbol];
}
function apply(obj, fn, args) {
obj = obj || window;
const symbol = Symbol();
obj[symbol] = fn;
obj[symbol](...args);
delete obj[symbol];
}
function bind(obj, fn) {
obj = obj || window;
return function (...args) {
fn.apply(obj, args);
}
}
var obj = {
name: 'ly'
}
function test(type) {
console.log(type, this.name)
}
call(obj, test, 'call');
apply(obj, test, ['apply']);
bind(obj, test)('bind');
</script>
1
https://gitee.com/ytiona/code-snippet.git
git@gitee.com:ytiona/code-snippet.git
ytiona
code-snippet
code-snippet
master

搜索帮助