javascript - 在包含模块的 Angular JS 文件中,函数和对象的顺序真的很重要吗?

标签 javascript angularjs

这是我的 Angular JS 应用程序中的 app.js:

var app = angular.module('store',[]); // [] is for dependencies which we don't have yet so it's empty

(function(){
    app.controller('StoreController',function(){
        this.blabla = student;
    });
})();



(function(){
    app.controller('ControllerForArrayOfStudents',function(){
        this.students = student1;
    });
})();

var student = 
{
    name:"Abc Def",
    rollNumber:12,
    isBrilliant: true,
    isMale:false,
    isFemale: false
}



var student1 = 
[
{
    name:"Abc Def",
    rollNumber:12,

},

{
    name:"Ghi",
    rollNumber:13,
}
]

除非我按如下方式更改函数和对象的顺序,否则效果很好:

var app = angular.module('store',[]); // [] is for dependencies which we don't have yet so it's empty

(function(){
    app.controller('StoreController',function(){
        this.blabla = student;
    });
})();

var student = 
{
    name:"Abc Def",
    rollNumber:12,
    isBrilliant: true,
    isMale:false,
    isFemale: false
}


(function(){
    app.controller('ControllerForArrayOfStudents',function(){
        this.students = student1;
    });
})();




var student1 = 
[
{
    name:"Abc Def",
    rollNumber:12,

},

{
    name:"Ghi",
    rollNumber:13,
}
]

我在更改订单时遇到的错误是: enter image description here

在包含模块的 Angular JS 文件中,函数和对象的顺序真的很重要吗?

最佳答案

如何修复

发生这种情况是因为变量声明后缺少所有分号。如果添加它们,效果就很好。 AngularJs 与此无关。

在第一种情况下它有效,因为 Chrome 的 JavaScript 解析器注意到 student 的对象初始值设定项之后的下一项是 var,它只能位于语句的开头,因此它假设这是一个新语句,并为您插入分号,感谢 automatic semicolon insertion .

但在第二个示例中它不能这样做,因为对象初始值设定项之后的 (有效,并且看起来像开头的 () 运算符执行函数调用,期望其前面的内容是函数引用。因此您需要分号,但无法为您插入:

var app = angular.module('store',[]); // [] is for dependencies which we don't have yet so it's empty

(function(){
    app.controller('StoreController',function(){
        this.blabla = student;
    });
})();

var student = 
{
    name:"Abc Def",
    rollNumber:12,
    isBrilliant: true,
    isMale:false,
    isFemale: false
};  // <======================= here

(function(){
    app.controller('ControllerForArrayOfStudents',function(){
        this.students = student1;
    });
})();

var student1 = [
  {
      name:"Abc Def",
      rollNumber:12,

  },

  {
      name:"Ghi",
      rollNumber:13,
  }
];

关于自动分号插入(ASI)

该标准声明 JavaScript 解释器必须在某些情况下在 token 流中插入分号,其中语法有些清晰并且涉及换行符。 (Automatic semicolon insertion) 在第一个片段中,所有缺失的分号都属于这些特殊情况,因为它们在没有分号的情况下无法形成有效的语法,因此将插入它们。 (该语句不能用另一个 var 或 eof 继续) 在第二个中,以下标记是 (,它是语法的某些产生式所允许的,并且不是受限制的产生式。 具体来说,它是第一种形式的 CallExpression,正如句法语法中所述,ObjectLiteralMemberExpression

P.S.:这个故事的寓意:使用分号。分号在语法中引入了冗余,编程语言需要良好的冗余水平才能注意到拼写错误。没有冗余的语言意味着任何随机的字符序列都是有效的代码。

关于javascript - 在包含模块的 Angular JS 文件中,函数和对象的顺序真的很重要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32606852/

相关文章:

javascript - 未捕获的语法错误 : Unexpected identifier - importing jquery

AngularJS 触摸轮播

javascript - 如何绑定(bind)由 AngularJS 中的函数构造函数创建的对象

javascript - ui-sref-active 用于嵌套状态

angularjs - 如何在 ng :pluralize 中格式化数字

javascript - Reactjs - 强制刷新的工作流程是什么?

javascript - 将 css "display:none"值替换为“显示 :flex using js

javascript - RactiveJS 可拖动使用鼠标事件

javascript - React-native 从 Camera Roll 和 Camera Roll API 获取所有照片

javascript - AngularJS:尝试使用服务,出现错误 "cannot read property ',然后是“未定义”