我不知道爲什麼我在控制檯中獲得undefined
。我閱讀了關於範圍變量的知識,並且知道在範圍外定義一個變量應該使該變量可以在特定函數範圍之外訪問。 不知道我在做什麼錯在這裏:未定義的值,變量範圍問題
const history = createHistory();
const location = history.location;
let loc;
const listen = history.listen((location) => {
loc = `${location.search}${location.hash}`;
return loc;
})
console.log(loc);
我的控制檯登錄undefined
請注意,'location'是'window'上的一個全局變量,您可以通過'window.location'或甚至'location'訪問它。 – JohnnyCoder