概念
this的指向在函數定義的時候是確定不了的,只有函數執行的時候才能確定this到底指向誰,實際上this的最終指向的是那個調用它的對象
1.只有函數調用,this只想window
function name() {
console.log(this)
}
name() 輸出window
2作為對象的方法調用,誰最后調用它,this指向誰。
function name() {
console.log(this.name)
}
function getName(name) {
this.name = name
}
var student = new getName("王")
name.call(student) 輸出王
3 構造函數this
構造函數創建出來的對象this就只想該對象
4 call
指向后面改變的對象