Comments (8)
bind函数多次调用会已第一次绑定的this为准,softbind已最后一次绑定传入的this为准;
from daily-question.
Function.prototype.softBind = function(obj, ...rest) {
const fn = this
const bound = function(...args) {
const o = !this || this === (window || global) ? obj : this
return fn.apply(o, [...rest, ...args])
}
bound.prototype = Object.create(fn.prototype)
return bound
}
function foo() {
console.log(name: ${this.name}
);
}
let obj = {name: "obj"};
obj2 = {name: "obj2"};
obj3 = {name: "obj3"};
let fooBJ = foo.softBind(obj);
fooBJ(); // name: obj 这个时候软绑定已经生效了,this绑定到obj上
obj2.foo = foo.softBind(obj);
obj2.foo(); //name: obj2 这里已经的this隐式绑定到obj2上了
fooBJ.call(obj3); // name: obj3 这里this被硬绑定到obj3上了
setTimeout(obj2.foo, 100); // name: obj 软绑定了最初的obj
from daily-question.
TODO
from daily-question.
bound.prototype = Object.create(fn.prototype); 为什么要有这一句,不是很懂
from daily-question.
Function.prototype.softBind = function (obj, ...args) {
const fn = this;
return function (...args2) {
return fn.apply(this === global ? obj : this, args.concat(args2));
};
};
from daily-question.
Function.prototype.softBind = function (ctx) {
ctx ??= globalThis
ctx = Object(ctx)
const self = this
const args = [...arguments].slice(1)
function bound() {
fn.call(new.target || this !== globalThis ? this : ctx, ...args)
}
bound.prototype = self.prototype
return bound
}
from daily-question.
bound.prototype = Object.create(fn.prototype); 为什么要有这一句,不是很懂
是要考虑到new的情况来着
from daily-question.
实现一个Bind:
function person(a, b, c, d, e) {
console.log(this.name, a + b + c);
console.log(d,e);
}
const yeti = {
name: 'yeti'
}
// 实现一个bind
Function.prototype.newmyBind = function (obj, ...args) {
const that = this
return function (...rest) { //返回一个修改好this的函数
// ...rest来自返回函数传入的参数 ...args是调用Bind时传入的参数
that.call(obj, ...args, ...rest)
}
}
const resBind = person.newmyBind(yeti, 1, 2, 6) //1 2 6 就是...args
resBind(5, 6) //传入...rest
from daily-question.
Related Issues (20)
- 多阶段构建并不需要 docker-compose HOT 1
- 【Q742】大文件上传,如何获取到读取进度? HOT 1
- 代码
- [feature request]面试题添加难度排序以及一键生成一份面试题的工具 HOT 2
- 【Q747】如何实现一个 omit/omitBy 函数 HOT 6
- 【Q748】在 babel 编译为低版本 ES 时,为何能够编译可选链之类语法,但无法编译 API HOT 1
- 【Q743】实现 batchFn 函数,可以批量执行函数 HOT 4
- 【开源自荐】推荐一个每日更新的前端面试题库 HOT 1
- 【Q474】在 react 中,以下父子组件的 useEffect/useLayoutEffect 顺序如何 HOT 1
- 【Q745】webpack 的打包流程是什么样的 HOT 1
- 【Q744】数据库中更新一条记录时,如何自动更新其 updated_at 字段
- 【Q746】如何避免 SQL 攻击
- 【Q749】React18 有哪些新特性
- 【Q750】React19 有哪些新特性
- 【Q752】实现 useDebouncedCallback
- 【Q753】你是如何学习前端的
- 【Q754】实现 LazyMan
- 【Q755】简述 WebWorker API
- 【Q756】Promise.race 与 Promise.any
- 【Q757】如何解决深拷贝问题中的循环引用
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from daily-question.