Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bind问题记录 #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions somequestion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//您好 最近很认真的再看你写的深入系列。在bind时候 发现了一个问题
var obj = {
value: '123'
}
function bar(name, age) {
return {
name: name,
age: age,
value: this.value
}
}
// 第四版
Function.prototype.bind2 = function (context) {

var self = this;
var args = Array.prototype.slice.call(arguments, 1);

var fNOP = function () {};

var fbound = function () {
var bindArgs = Array.prototype.slice.call(arguments);
self.apply(this instanceof self ? this : context, args.concat(bindArgs));
}
fNOP.prototype = this.prototype;
fbound.prototype = new fNOP();
return fbound;

}

bar.bind2(obj)() //undefined
bar.bind(obj)() //{name: undefined, age: undefined, value: "123"}

//两者结果有差异,其原因是在fbound中实现之后,没有返回该函数。如有考虑不正确的地方,还望指正