我正在经历一些快节奏,而不是我发现这是绑定和调用/应用之间的区别。
Course.create(name: "Course 1", topics: [(t1, my_column: 1), (t2, my_column: 2)])
然后他们提到了这个例子
var colt = {
firstName: "rohit",
sayHI: function() {
setTimeout(function(){
console.log("hi" + this.firstName)
}, 3000)
}
}
出于某种原因,我无法理解,有人可以帮我理解吗?
答案 0 :(得分:1)
编辑:
let colt = {
firstName: "rohit",
sayHI: function() {
setTimeout(
/* 1. Create a new function. */
function() {
/* 3. The 'this' = colt object. */
console.log("hi" + this.firstName)
}
/* 2. Make a copy of the function and set the inside 'this' keyword with a colt object.
And when you call 'this' inside the function is this colt object. */
.bind(this), 500);
}
}.sayHI(); //this would be evoked when the function runs