var obj = {
print: function(){
console.log(this)
}
}
var stolenFunction = obj.print
obj.print() // this -> object { print: [Function: print] }
stolenFunction() // this -> `window`Please explain why the values of the last two lines of the function are different ?
Actually , func.call(environment, p1, p2) is normal way to call function but func() or object.child.func()
so:
obj.print()is equal toobj.print.call(obj)print()is equal toprint.call(window)
Example:
obj.print.call(window) // window
obj.print.call(obj) // objIf the context you pass is null or undefined, then the window object is the default context (the default context in strict mode is undefined)
obj.print.call() // window, not objfunc.call(environment, p1, p2)
this = environment