函数和对象的顺序在包含模块的Angular JS文件中是否真的重要? [英] Does order of functions and objects really matter in Angular JS file containing module?

查看:48
本文介绍了函数和对象的顺序在包含模块的Angular JS文件中是否真的重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here is app.js from my Angular JS App:

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,
}
]

除非我按如下方式更改函数和对象的顺序,否则此方法工作正常:

This works fine unless I change the order of functions and objects as follows:

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,
}
]

我在更改订单时遇到的错误是这样的:

Error I am getting on changing order is this:

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

Does order of functions and objects really matter in Angular JS file containing modules?

推荐答案

如何修复

之所以发生这种情况,是因为在变量声明之后您丢失了所有分号.如果添加它们,则可以正常工作.AngularJs与它无关.

How to fix it

That happens because you're missing all the semicolons after the variable declarations. If you add them, it works fine. AngularJs has nothing to do with it.

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

In the first case it works, because the JavaScript parser of Chrome notices that the next term after the object initializer for student is var, which could only stand at the beginning of a statement, so it assumes that it is a new statement and inserts the semicolon for you thanks to automatic semicolon insertion.

但是在您的第二个示例中它无法做到这一点,因为对象初始化程序之后的( valid ,并且看起来像开头的()运算符,期望它前面的内容可以作为函数引用.因此,您需要使用分号,无法为您插入分号:

But it can't do that in your second example, because the ( after the object initializer is valid and looks like the beginning () operator that does a function call, expecting what's in front of it to be a function reference. So you need the semicolon, it can't be inserted for you:

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会在令牌流中解释必须插入分号,在这种情况下,语法有点清晰,并且涉及到换行符.(自动分号插入)在第一个代码段中,所有缺失的分号都是特殊情况,因为没有分号就不会形成有效的语法,因此将它们插入.(该语句不能以另一个 var 或eof继续)在第二个中,以下标记是(,这是语法的某些产生形式所允许的,并且不是限制性产生形式.具体来说,它是第一种形式的 CallExpression ,因为 ObjectLiteral MemberExpression ,如语法语法中所述.

About automatic semicolon insertion (ASI)

The standard declares that JavaScript interpretes must insert semicolons in the token stream in certain situations, where the syntax is somewhat clear and a newline is involved. (Automatic semicolon insertion) In the first snippet all the missing semicolons are in those special cases, as they do not form a valid grammar without semicolons, so they are inserted. (The statement cannot continue with another var, or an eof) In the second one, the following token is a (, which is allowed by some production of the grammar, and is not a restricted production. Specifically it is a CallExpression, of the first form, as an ObjectLiteral is a MemberExpression, as stated in the syntactic grammar.

P.S .:故事的寓意:使用分号.分号在语法中引入了冗余,并且编程语言需要一个健康的冗余级别,以便能够注意到拼写错误.没有冗余的语言意味着任何随机的字符序列都是有效的代码.

P.S.: Moral of the story: use semicolons. Semicolons introduce redundancy in the grammar, and programming languages need a healthy level of redundancy in order to be able to notice typos. A language with no redundancy would mean that any random sequence of characters would be a valid code.

这篇关于函数和对象的顺序在包含模块的Angular JS文件中是否真的重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆