angularjs - 由于缩进,Angular 1 中出现错误
问题描述
我是 Angular JS 的新手,正在努力学习。尝试编写一个提供程序,它的行为方式很奇怪并引发错误。您能否帮助确定错误的原因。
这是运行良好的代码:
myapp.provider('Testing',function(){
return {
$get: function() {
return {
add: function(a,b){return a+b;}
};
}
};
});
以下是无法正常工作的代码:
myapp.provider('Testing',function(){
return {
$get: function() {
return
{
add: function(a,b){return a+b;}
};
}
};
});
由于第二个片段的错误:
未捕获的语法错误:意外的令牌(
错误指向这个语句:add: function(a,b){return a+b;}
两个代码片段的唯一区别是{
$get 定义函数中返回语句的“”位置。
是否有任何正当理由说明“ {
”的位置很重要。
解决方案
Yes, the placement of "{" after return matters. It is a default JavaScript behavior to close a statement automatically at the end of a line. So,
return
{
add: function(a,b){return a+b;}
};
The function will return undefined and the next parentheses will be act as Unexpected token
return ;
{
add: function(a,b){return a+b;}
};
推荐阅读
- jquery - 使用 jquery 输入类型文件的 base64 字符串
- asp.net-core-2.0 - 如何在 Asp.NET ZERO / Boilerplate 中迁移租户数据库和主机数据库?
- ruby-on-rails - Ruby on Rails 分离管理文件夹、控制器和布局
- javascript - 我可以在 Vue js 中同时使用 SASS 和 LESS
- c++ - g++/clang++ 反转编译错误顺序
- go - 任何人都可以理解 connStateInterface 吗?
- python-3.x - 如何使用聊天机器人的 API 将 sqlite3 转换为 csv 格式?
- reactjs - 未为 ES6 设置默认道具作为默认值
- javascript - 映射默认值
- javascript - OnClick() 函数在 JavaScript 中不起作用