Comments (10)
Object.assign(Function.prototype, {
bind2: function (thisArg, ...arg) {
return (...params) => (thisArg.fn = this, thisArg.fn(...arg, ...params))
}
})
from technology-blog.
ES6版本
Function.prototype.bind2 = function() {
var fn = this;
var argsParent = [...arguments];
return function() {
fn.call(...argsParent, ...arguments);
};
}
`
from technology-blog.
为什么还要有下面这一步呢?
var args = argsParent.concat(Array.prototype.slice.call(arguments)); //转化成数组
直接_this.apply(context, argsParent);试了下也能运行~
from technology-blog.
为什么还要有下面这一步呢?
var args = argsParent.concat(Array.prototype.slice.call(arguments)); //转化成数组
直接_this.apply(context, argsParent);试了下也能运行~
我也想问这个问题;
const app = {
name: 'app',
getName(version) {
console.log(this.name + ': ' + version)
}
};
Function.prototype.bind2 = function () {
var fn = this;
var argsParent = [...arguments];
return function () {
fn.call(...argsParent);
};
};
app.getName.bind2({ name: 'hello' }, 1.1)();
// 等价 app.getName.bind2.call(app.getName, { name: 'hello' }, 1.1);
此时bin2内,获取的上下文应该为this,也就是 fn 是为 app.getName 这个函数, 此时要让其绑定上下文至 arguments 的第一个参数,我觉得上述就可以了呢,是我疏忽了什么嘛?
好吧,我懂了
return function () {
fn.call(...argsParent, ...arguments);
};
// 这里后面的一个 arguments 是 return 的函数的 arguments,不是外层函数的;
// 我之前弄混淆了,之所以需要合并这个参数,是因为会有下面这种调用
// 不这么做的话,新的函数就无法接受参数
// bind 宗旨就是返回一个,重新绑定上下文的函数
app.getName.bind2({ name: 'hello' }, 1.1)(2.2);
from technology-blog.
为什么还要有下面这一步呢?
var args = argsParent.concat(Array.prototype.slice.call(arguments)); //转化成数组
直接_this.apply(context, argsParent);试了下也能运行~
有一种情况,在bind绑定函数的时候传实参,然后在调用bind返回的函数的时候,又传一次实参。
这里就是为了解决这种情况,
比如:
function f(a, b) {
console.log(a,b)
console.log('this', this)
}
var obj = {name: 'william', age: 23}
f.bind(obj, 'a')('b')
其实还有一种情况需要考虑,bind函数的返回函数在作为构造函数,使用new创建对象时,提供的this值绑定会失效,可以使用如下方案。具体请看
Function.prototype.bind = function(context) {
var context = context || window;
var self = this;
var bindArgs = [].slice.call(arguments, 1);
function NOP() {};
NOP.prototype = this.prototype;
var Bound = function() {
return self.apply(this instanceof NOP ? this : context, bindArgs.concat([].slice.call(arguments)))
}
Bound.prototype = new NOP();
return Bound;
}
from technology-blog.
不使用 call apply方法
Function.prototype.bind = function() {
const context = arguments[0] || window
const argument = Array.from(arguments).splice(1, arguments.length - 1)
return (()=>{
context.fn = this
context.fn(...argument)
delete context.fn
})
}
from technology-blog.
不使用 call apply方法
Function.prototype.bind = function() { const context = arguments[0] || window const argument = Array.from(arguments).splice(1, arguments.length - 1) return (()=>{ context.fn = this context.fn(...argument) delete context.fn }) }
应该是这样吧
Function.prototype.bind2 = function () {
const context = arguments[0] || window
const argument = Array.from(arguments).splice(1, arguments.length - 1)
context.fn = this;
return function () {
context.fn(...argument, ...arguments);
delete context.fn
}
}
from technology-blog.
参考 《JavaScript高级程序设计》函数绑定:
```js
Function.prototype.bind1 = function(context) {
var that = this
return function() {
that.apply(context, arguments)
}
}
var person = { name:"杨俊宁" }
function sayHi(name) {
console.log(this, name)
}
sayHi.bind1(person)() // {name: "杨俊宁"} undefined
from technology-blog.
ES6版本
Function.prototype.bind2 = function() { var fn = this; var argsParent = [...arguments]; return function() { fn.call(...argsParent, ...arguments); }; } `
为什么要用ES6版本呢,==
from technology-blog.
不使用 call apply方法
Function.prototype.bind = function() { const context = arguments[0] || window const argument = Array.from(arguments).splice(1, arguments.length - 1) return (()=>{ context.fn = this context.fn(...argument) delete context.fn }) }
第二个arguments有问题,箭头函数没有自己的arguments
from technology-blog.
Related Issues (20)
- 33.如果一个实习生,他本地git的A分支被误删了, A分支代码没有被push到远程,如何找到之前A的提交记录和代码 HOT 4
- 34.如果你用nodejs实现的爬虫服务器的IP被指定网站封了,如何解封? HOT 2
- 35.用nodejs,将base64转化成png文件 HOT 1
- 36.请设计一个方案:有a、b、c三个npm插件,它们会经常更新,在前端项目npm run start启动后,要求a、b、c三个npm插件自动更新到最新版本 HOT 2
- 37.大文件转存问题:服务器A有一个1000G的文件, 需要通过服务端B转发到服务端C,但是服务器B内存只有1个g, 怎么去实现这个大文件转存 HOT 4
- 小蝌蚪传记:PNG图片压缩原理--屌丝的眼泪 HOT 1
- KOA2框架原码解析和实现
- 前端人工智能:通过机器学习推导函数方程式
- webpack工程化打包原理解析与实现
- 小蝌蚪传记:git时光穿梭机--女神的侧颜 HOT 1
- 小蝌蚪传记:端口转发——夜店传说 HOT 4
- 小蝌蚪传记:200行代码实现前端无痕埋点——顶级渣男 HOT 3
- 小蝌蚪传记:nodejs线上模块热部署原理与实现——富婆的爱
- 小蝌蚪日记:通过console.log高仿FBI Warning
- 小蝌蚪传记:前端菜鸟让接口提速60%的优化与原理
- 小蝌蚪系列:面试中软性问题的套路与反套路(上) HOT 4
- 小蝌蚪传记:js、css、html压缩与混淆汇总 --> 变弯记
- 小蝌蚪传记:前端实用技巧,通过babel精准操作js文件——暗恋
- 【开源自荐】推荐一个每日更新的前端面试题库 HOT 3
- 【开源自荐】SolidUI 一句话生成任何图形 HOT 3
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 technology-blog.