Console.log显示对象,但递归调用函数时得到错误
console.log shows object but when recursively calling function gets error
下面是我的代码:
function makeContent(jsonData){
var aProperty, containerType, contentContainerName, containerIdentifier, containerComment, theContent ;
for(aProperty in jsonData){
switch(aProperty){
case "containerType": containerType = jsonData[aProperty];
case "contentContainerName" : contentContainerName = jsonData[aProperty];
case "containerComment" : containerComment = jsonData[aProperty];
case "containerIdentifier" : containerIdentifier = jsonData[aProperty];
case "itemContent" : theContent = jsonData[aProperty];
}
}
if(theContent.hasOwnProperty){
console.log(theContent);
makeContent(theContent);
}
我得到这个作为我的输出:
(对象)footer.js: 59
TypeError: 'undefined'不是一个对象(求值'theContent.hasOwnProperty') footer.js:58
这对我来说没有意义,因为当我console.log(内容)我得到一个对象,它工作得很好。只有在尝试递归调用makeContent函数时添加该函数时才会出现错误。由于这个错误,我没有添加返回语句,我应该这样做吗?
您似乎正在使用条件表达式if(theContent.hasOwnProperty)
来确定是否定义了theContent
。变量在函数的顶部声明,并且只在最后的case
中定义。
检查变量是否定义的最可靠的方法如下:
if (typeof theContent !== 'undefined`) { ... }
当最后的case
语句没有执行时,theContent
没有定义,并且试图访问未定义值的hasOwnProperty
将导致观察到的错误。
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- Javascript搜索函数错误
- Chrome Javascript日期构造函数错误
- 浏览器中出现javascript初始大写函数错误
- Createjs冲突问题localToLocal不是函数错误
- Java脚本函数错误
- 更新带有Javascript函数错误的变量
- 如何将对象插入对象列表的开头,当它有 unshift 不是函数错误
- 在将 Pickadate 与 Meteor 一起使用时,未定义不是函数错误
- 运行咕噜咕噜咕噜抛出 _.object 不是函数错误
- 了解函数(错误、数据)回调
- javascript中的typeof()函数错误
- 我一直在获取angularjs服务不是函数错误(LoginService.login不是函数)
- HTML5拖拽&drop正在抛出undefined不是函数错误
- Google Maps API a.lat不是函数错误
- getItemAtIndex不是函数错误
- Javascript停止函数错误
- 对话框函数错误无效函数
- Javascript 自定义函数错误
- 因果报应单元测试'未定义'不是函数错误