call, apply, bind
Call
function a(){
console.log('a');
}
// These two are the same
a() === a.call()
Questions
var b = {
name: 'jay'
say(): {
console.log(this)
}
}
var c = {
name: 'jay'
say(): {
return function() {
console.log(this)
}
}
}
var d = {
name: 'jay'
say(): {
return () => console.log(this)
}
}
// What would each return?
b.say()
c.say()()
d.say()()
var b = {
name: 'jay'
say(): {
console.log(this)
}
}
b.say()
b.say()
would return {name: "jay", say: fn()}
Because the say()
function is "bounded" to the b
object
var c = {
name: 'jay'
say(): {
return function() {
console.log(this)
}
}
}
c.say()()
This would return window
that's because the function isn't bounded to anything. As in it's .
is not called by anything.
c.say()
is run, and it'd equal something like var temp = c.say()
and then temp()
would then be executed. And since temp
isn't bounded to anything it'd just be the window object.
var d = {
name: 'jay'
say(): {
return () => console.log(this)
}
}
d.say()()
This would return {name: 'jay', say: fn}
Although it might look the same as c
the main difference is that arrow function is lexically scopes the function
Last updated