function exampleFunction(){
var theVariable = "Lol!";
var variable2 = Lol.toLowerCase();
console.log(theVariable);
delete theVariable; //to prevent bugs, I want to ensure that this variable is never used from this point onward.
console.log(theVariable); //This still prints "Lol!", even though I just tried to delete the variable.
}
在JavaScript中,是否可以防止某個變量在某個點後被用於某個函數?我試過聲明一個名爲theVariable
的字符串,然後我嘗試使用delete theVariable
刪除該變量,但console.log(theVariable)
仍然會打印theVariable
的值,即使在該點之後。如何防止變量在某個點之後被使用?
我嘗試使用delete theVariable
使theVariable
不可用,從那時起(爲了防止我不再需要使用該變量時不小心使用該變量),但似乎沒有這種效果。有什麼辦法可以解決這個限制嗎?
反正你不能在函數外面使用它,所以當函數返回時var就不見了。您可以將其設置爲未定義,但詞彙名稱將始終從功能的詞法範圍中劃分出來。 – dandavis
@dandavis「it」指的是什麼? –
theVariable = undefined;儘可能接近你想要的東西。 – dandavis