如果我在 Javascript 中有以下代码:
import getTranslation from "some-library";
import getUserSettings from "some-other-library";
const getTranslatedLabel = () => {
const translatedLabel = getTranslation("cheeseburger");
const { enableTranslatedLabels } = getUserSettings();
if (enableTranslatedLabels) {
return translatedLabel;
} else {
return "";
}
}
像 V8 这样的 javascript 引擎是否足够智能,仅在enableTranslatedLabels 为
getTranslation
时才执行 true
函数?
这和V8无关,更多的是和你的业务逻辑有关。
您需要重新排列代码,以便仅在启用时执行该功能。
import getTranslation from "some-library";
import getUserSettings from "some-other-library";
const getTranslatedLabel = () => {
const { enableTranslatedLabels } = getUserSettings();
//if not enabled, we will not executed getTranslation
if( enableTranslatedLabels ){
return "";
}
//this will be executed and value returned because it is enabled.
return getTranslation("cheeseburger");
}