![](/img/trans.png)
[英]In javascript ,Access to global variable whenever its name is same with local variable?
[英]Access global variable with same name as instance variable
var myVar = 5;
var example = function() {
var myVar = 10;
// How can I access the global variable 10?
};
如果存在與局部變量同名的全局變量,如何訪問全局變量?
我沒有使用es6。
我在jsdb中運行它,而不是在瀏覽器或節點中運行,因此我無法訪問“窗口”或“全局”對象
假設您可以更改調用example()
函數的代碼,則應該能夠使用Function.prototype.call()
傳遞當前作用域,並使用this
訪問。 例如
'use strict' var myVar = 5; var example = function() { var myVar = 10; console.info('Local:', myVar) console.info('Outer:', this.myVar) }; example.call({ myVar })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.