setTimeout this
this
this this
setTimeout this
let user = {
firstName: "John",
sayHi() {
alert(`Hello, ${this.firstName}!`);
}
};
setTimeout(user.sayHi, 1000); // Hello, undefined!
this.firstName John undefined
setTimeout user.sayHi
let f = user.sayHi;
setTimeout(f, 1000); // user
setTimeout this=window Node.jsthis timer this.firstName window.firstName this undefined
1
let user = {
firstName: "John",
sayHi() {
alert(`Hello, ${this.firstName}!`);
}
};
setTimeout(function() {
user.sayHi(); // Hello, John!
}, 1000);
user
setTimeout(() => user.sayHi(), 1000); // Hello, John!
setTimeout user
let user = {
firstName: "John",
sayHi() {
alert(`Hello, ${this.firstName}!`);
}
};
setTimeout(() => user.sayHi(), 1000);
// user 1
user = {
sayHi() { alert("Another user in setTimeout!"); }
};
// Another user in setTimeout!
2bind
bind this
//
let boundFunc = func.bind(context);
func.bind(context) exotic objecttransparently func this=context
boundFunc this func
funcUser func this=user
let user = {
firstName: "John"
};
function func() {
alert(this.firstName);
}
let funcUser = func.bind(user);
funcUser(); // John
func.bind(user) func bound this=user
arguments func
let user = {
firstName: "John"
};
function func(phrase) {
alert(phrase + ', ' + this.firstName);
}
// this user
let funcUser = func.bind(user);
funcUser("Hello"); // Hello, John "Hello" this=user
let user = {
firstName: "John",
sayHi() {
alert(`Hello, ${this.firstName}!`);
}
};
let sayHi = user.sayHi.bind(user); // (*)
//
sayHi(); // Hello, John!
setTimeout(sayHi, 1000); // Hello, John!
// user 1
// sayHi pre-bound user
user = {
sayHi() { alert("Another user in setTimeout!"); }
};
(*) user.sayHi usersayHi bound setTimeout
arguments this bind
let user = {
firstName: "John",
say(phrase) {
alert(`${phrase}, ${this.firstName}!`);
}
};
let say = user.say.bind(user);
say("Hello"); // Hello, John! "Hello" say
say("Bye"); // Bye, John! "Bye" say
bindAllfor (let key in user) {
if (typeof user[key] == 'function') {
user[key] = user[key].bind(user);
}
}
JavaScript lodash _.bindAll(object, methodNames)
Partial functions
this
thisarguments
bind
let bound = func.bind(context, [arg1], [arg2], ...);
this
mul(a, b)
function mul(a, b) {
return a * b;
}
bind double
function mul(a, b) {
return a * b;
}
let double = mul.bind(null, 2);
alert( double(3) ); // = mul(2, 3) = 6
alert( double(4) ); // = mul(2, 4) = 8
alert( double(5) ); // = mul(2, 5) = 10
mul.bind(null, 2) double mul null 2 arguments
this bind null
triple
function mul(a, b) {
return a * b;
}
let triple = mul.bind(null, 3);
alert( triple(3) ); // = mul(3, 3) = 9
alert( triple(4) ); // = mul(3, 4) = 12
alert( triple(5) ); // = mul(3, 5) = 15
doubletriple
send(from, to, text) user send user sendTo(to, text)
partial
arguments this
bind arguments
arguments partial
function partial(func, ...argsBound) {
return function(...args) { // (*)
return func.call(this, ...argsBound, ...args);
}
}
//
let user = {
firstName: "John",
say(time, phrase) {
alert(`[${time}] ${this.firstName}: ${phrase}!`);
}
};
// partial
user.sayNow = partial(user.say, new Date().getHours() + ':' + new Date().getMinutes());
user.sayNow("Hello");
//
// [10:00] John: Hello!
partial(func[, arg1, arg2...]) (*) func
-
thisuser.sayNowuser -
...argsBoundpartial"10:00" -
...args"Hello"
spread
lodash _.partial
func.bind(context, ...args) func bound this ...args
bind this setTimeout
partially applied partial
send(from, to) from
<code><pre>10 plnkrJSBincodepen