我很難找到我的頭。 我知道範圍鏈接,javascript中的回調函數,其值爲這個中的回調函數和箭頭函數。javascript這個封閉的外部函數有一個綁定'this'
在JavaScript中,閉包可以通過作用域鏈訪問封閉函數的變量。那麼爲什麼閉包不能通過Function.prototype.bind訪問'this'的綁定函數?變量「this」不是範圍鏈的一部分嗎?
冉鉻控制檯中下面的代碼:
a = 4;
b = 6;
function outer(){
function inner(){
console.log(`this.a is ${this.a} and this.b is ${this.b}`);
}
inner();
}
outer.bind({a:1,b:3})()
和控制檯掀開:
this.a is 4 and this.b is 6
這是因爲你有全局變量! inner內部的'this'仍然是訪問全局變量的'window'。 – Li357
@AndrewLi好吧,我明白了,不應該內部訪問外部的綁定a和b,因爲它是一個閉包? – basum
不,因爲你在封閉中訪問'this'。它與外部功能無關。 – Li357