0
此代码不会通过测试为什么我的typeof检查未定义我的摩卡测试失败?
功能:
function setTheme(theme) {
chartTheme = theme;
}
function getTheme() {
if (typeof chartTheme === undefined) {
chartTheme = 'dark';
}
return chartTheme;
}
测试:
但是这并经过测试,如果我检查为"undefined"
作为一个字符串。
function getTheme() {
if (typeof chartTheme === 'undefined') {
chartTheme = 'dark';
}
return chartTheme;
}
谢谢,我想我不明白,还是那个'本身undefined'是一种!猜猜我的代码将传递给函数的'undefined'转换为字符串。哦,我也只是用这些基本的默认检查更简单地更新我的代码:'theme = theme || 'dark';' –
@LeonGaban因为变量的值并未实际定义。所以,应该有一些可以说明这一点的价值。另外,你可以使用ES6的默认参数作为函数setTheme(theme ='dark'){'而不是'|| “dark''。 – Tushar
感谢那个ES6提示!我试过了,但在终端PhantomJS 2.1.1(Mac OS X 0.0.0)中出现这个错误。ERROR SyntaxError:Expected token')''我想我需要某种巴别解释器? –