不知道這是否是命名此問題的最佳方式,但我有以下代碼,它不喜歡在嚴格模式下運行。我需要設置runstatementshere()
函數的內容/語句,具體取決於if
條件。然後runstatementshere()
將作爲另一個函數的一部分運行併成爲該範圍的一部分。Javascript - 如何選擇性地執行函數/語句
if (i == true) {
function runstatementshere() {
// multiline commands
};
}
else {
function runstatementshere() {
}
}
function somecode() {
runstatementshere();
}
爲什麼不把'if'聲明'runstatementshere'函數內? – 4castle
使用函數表達式將函數分配給在更高範圍聲明的變量而不是函數聲明。然後,您可以根據您的if語句爲變量分配不同的函數實現。 – jfriend00
你不應該在條件內定義一個函數 –