您好,欢迎来到意榕旅游网。
搜索
您的当前位置:首页AS3开发代码规范

AS3开发代码规范

来源:意榕旅游网
编码规范 Version 1.0

AS3编码规范

2011年5月20日

编码规范 Version 1.0

目 录

命名规范 ........................................................................................................................................... 1

1. 缩略词 ......................................................................................................................... 1 2. 缩略词组 ..................................................................................................................... 1 3. 词组的连接 ................................................................................................................. 2 4. 包含类型说明含义的命名 ......................................................................................... 2 5. 包名 ............................................................................................................................. 2 6. 文件名 ......................................................................................................................... 3 7. 命名空间 ..................................................................................................................... 3 8. 接口命名 ..................................................................................................................... 3 9. 类名 ............................................................................................................................. 3 10.事件名 ......................................................................................................................... 3 11. 样式名 ....................................................................................................................... 3 12. 字符串类型的枚举命名 ........................................................................................... 4 13. 常量命名 ................................................................................................................... 4 14. 属性命名,setter与getter的命名 .................................................................... 4 15. 存储型变量命名 ....................................................................................................... 5 16. 函数命名 ................................................................................................................... 5 17. 事件处理函数命名 ................................................................................................... 5 18. 参数名 ....................................................................................................................... 5 19. 其他常见命名规范 ................................................................................................... 6

语言规范 ........................................................................................................................................... 6

1. 编译参数 ..................................................................................................................... 6 2. 采用基于属性的函数命名 ......................................................................................... 6 3. 类型声明 ..................................................................................................................... 6 4. 内置类型常用字母书写规范 ..................................................................................... 7 5. 表达式 ....................................................................................................................... 10 6. 语句 ........................................................................................................................... 13 7. 声明 ........................................................................................................................... 19

格式化............................................................................................................................................. 23

1 行宽 ........................................................................................................................... 23 2 缩进 ........................................................................................................................... 23 3 声明 ........................................................................................................................... 23 4 数组脚标 ................................................................................................................... 23 5 逗号 ........................................................................................................................... 23 6 数组 ........................................................................................................................... 24 7 Object对象 .............................................................................................................. 24 8 类型声明 ................................................................................................................... 25 9 操作符 ....................................................................................................................... 26 10 语句 ......................................................................................................................... 26

注释................................................................................................................................................. 27

命名规范 1. 缩略词

在编程过程中,应该避免滥用缩写,比如 我们应该采用calculateOptimalValue(),而不应该取名叫 calcOptVal()

相比简短的却不知所云的命名,拥有清晰的词义的词更重要。同时你的搭档也不必记一些你创造出来的诸如“qualified” , “qual” 或者 “qlfd”之类的词。

但是一些公认的缩写还是必要的,我们在这里列出了一些常见的缩写:

          

acc 代替 accessibility, 比如:ButtonAccImpl auto 代替automatic, 比如: autoLayout auto 代替 automatic, 比如: autoLayout

eval 代替evaluate, 比如: EvalBindingResponder impl 代替implementation, 比如: ButtonAccImpl info 代替information, 比如: GridRowInfo num 代替number of, 比如: numChildren min 代替minimum, 比如: minWidth max 代替maximum, 比如: maxHeight nav 代替navigation, 比如: NavBar

regexp 代替regular expression, 比如: RegExpValidator

util 代替utility, 比如: StringUtil

以上缩略词只包括了现有公认缩略词的一部分。如果你正考虑使用一些这里没有列举出来的缩略词,最好先在代码里面全局搜索一下有没有人已经在用这个词了。如果没有的话,你可能需要考虑一下你要用的这个缩略词是否合适

对于同一个词,我们有可能用它的缩略词,但是却在另外一个地方却用原词。比如我们经常用“ horizontalScrollPolicy”与“verticalScrollPolicy”,但是却在“HBox ”与“VBox”里用它们的缩写“H”与“V”。这两者都是可以的,看实际情况而定。

2. 缩略词组

在AS3中,各种缩略词组随处可见,比如AIR, CSS, HLOC, IME, MX, MXML, RPC, RSL, SWF, UI, UID, URL, WSDL, XML等等

1

An acronym is always all-uppercase or all-lowercase (e.g., SWF or swf, but never Swf). The only time that all-lowercase is used is when the acronym is used by itself as an identifier, or at the beginning of an identifier, and the identifier should start with a lowercase letter. See the rules below for which identifiers should start with which case.

缩略词组通常用全部大写或者全部小写(比如SWF或者swf,但是Swf是不允许的).全部小写的缩略词组通常只在只包含缩略词组的变量,或者必须以小写开头的变量里面.比如uid,imeMode,其余时候就应该用大写,比如CSSStyleDeclaration, IUID,IIME.

3. 词组的连接

当一个标示符由若干单词组成的时候,可以有两种方式把单词拼接起来—驼峰式(LayoutManager)与下划线式(object_proxy)

4. 包含类型说明含义的命名

如果你想在标示符里面包含一个类型来说明标示符的含义,那必须在标示符的末尾用一个单词来说明.切忌不要使用在AS1里面用诸如_mc之类的词夹在在标示符里面.打个比方.要给一个说明一个边框样式是Shape类型的你可能使用” borderSkin”或者” borderShape”之类的词,但千万别用 ” notborder_mc”

Often, the best name for an object is simply the same as its type, with different casing: 通常,最好的类型说明方式是直接用类型的小写单词来做标示符. var button:Button = new Button();

5. 包名

一般我们用小写字母开始的驼峰式的方式来给包命名,最好使用名词或者动名词,而不要使用动词,副词,或者形容词。比如: controls, listClasses. 如果一个包名实包含了若干实现了相似功能的类,那就用一个名词的复数形式来做包名,比如

charts, collections, containers,controls, effects, events, formatters, managers, preloaders, resources, skins, states, styles, utils, validators.

如果一个包包含了实现了一类功能的类,那就用动名词来做包名,比如 binding, logging, messaging, printing. Otherwise, they are generally \"concept nouns\": accessibility, core, graphics, rpc.

如果一个包里面的类都支持同一个组件,比如FooBar,那就取名叫fooBarClasses

2

6. 文件名

对于可以导入的类,文件名与公开的API名(比如构造函数)必须一样,但是用嵌入的方式导入的可以不遵循这条规则。

用嵌入方式导入的类名,必须采用大写字母开头,并且用驼峰式方式命名。比如[Style(...)]标签里面的“Styles”: BorderStyles.as, ModalTransparencyStyles.as. 对于独立的资源,则采用小写的下划线式的方式来命名,比如: words: icon_align_left.png.

7. 命名空间

命名空间必须采用小写字母,并且用下划线方式来命名。 比如: mx_internal, object_proxy.

8. 接口命名

接口都必须使用字母I开头,并且用驼峰式的方式来命名 比如: IList, IFocusManager, IUID

9. 类名

类名一律采用大写字母开头以及驼峰式的命名方式命名 比如: Button, FocusManager, UIComponent. 子类一般以父类的命名来结尾,比如: Event 的子类FooBarEvent. Error 的子类FooBarError.

10.事件名

事件名一般采用小写字母开头,并且采用驼峰式的命名方式命名 比如: \"move\.

11. 样式名

样式名一般采用小写字母开头,并且采用驼峰式的命名方式命名

3

比如: color, fontSize.

12. 字符串类型的枚举命名

此类命名一般采用小写字母开头,并且采用驼峰式的命名方式命名 比如: \"auto\

13. 常量命名

常量命名全部采用大写字母,并且用下划线的方式连接多个单词。 变量名必须与值的单词一致,只是大小写不一样而已。比如: public static const FOO_BAR:String = \"fooBar\";

14. 属性命名,setter与getter的命名

属性,setter与getter的命名一般采用小写字母开头,并且采用驼峰式的命名方式命名 一般在for循环的第一层里面使用i做临时变量,用j做第二层做临时变量 for (var i:int = 0; i < n; i++) {

for (var j:int = 0; j < m; j++) {

... } }

在for-in的循环里面一般采用p来标示属性,比如: for (var p:String in o) {

... }

如果在子类复写父类的getter/setter方法,但是又想保持父类的getter/setter能用,那应该用一个美元符号$开头的单词来命名。并且这个方法应该标记为final,并且不应该做比父类的getter/setter方法更多的事情。

mx_internal final function get $numChildren():int {

return super.numChildren; }

4

15. 存储型变量命名

如果一个getter/setter方法是foo,那相对应的存储变量(私有变量)应该为_foo

16. 函数命名

函数命名一般采用小写字母开头,并且采用驼峰式的命名方式命名,并且一般采用动词。 words: measure(), updateDisplayList().

偏向属性设置类型的函数不要使用getFooBar()或者setForBar(),用getter/setter代替就可以了。但是在有些情况下,getFooBar()可能是实现了太多功能的大型函数,那尽量采 用 findFooBar(),calculateFooBar(), determineFooBar()之类的命名。

17. 事件处理函数命名

事件处理函数通常采用Handker结尾,比如: mouseDownHandler(). 如果事件是从子对象里面发出来的,那用处理函数采用子对象的名字开头,并且用下划线把处理函数名连接起来,比如:textInput_focusInHandler()

18. 参数名

在setter函数里面通常使用value作为参数名: Do this:

public function set label(value:String):void

Not this:

public function set label(lab:String):void

Or this:

public function set label(labelValue:String):void

Or this:

public function set label(val:String):void

使用event而非e,evt,或者eventObj之类的作为事件处理函数的命名:

5

protected function mouseDownHandler(event:Event):void

19. 其他常见命名规范

1、避免使用object之类的词义模糊的词

2、item一般当作数据对象而非显示对象来看待 3、renderer一般当作显示对象来看待

4、type代表as3的类型,其他的类型请用kind代替

语言规范 1. 编译参数

编译参数一律采用-开头,比如:-strict,-show

2. 采用基于属性的函数命名

尽量多采用基于属性的方法,比如getter/setter,因为在MXML的标签式的编程过程中,基于属性的方法更符合MXML的编程思路

3. 类型声明

常量,变量,参数,函数返回值都必须声明类型,即便是一个无类型的值,也要声明为*。比如:

var value:*,千万别写成 var value;

类型范围越小越好,比如,循环脚标必须采用int型,而非Number型,采用具体类型,而不要用Object或者*类型。mouseDownHandler函数的参数要用MouseEvent,而不要用Event

就算是一个非负整数,也尽量声明为整数。Uint只在RGB的颜色值,位运算与其他非数值类型里面用到 *只在一个值可能为undefined的时候用,如果一个值在未出生之前为null,那就用Object类型,而不要用*

在数组Array的声明后面,必须加注释/*元素类型*/

6

Do this:

var a:Array /* of String */ = [];

Not this:

var a:Array = [];

And this:

function f(a:Array /* of Number */):Array /* of Object */ {

... }

Not this:

function f(a:Array):Array

3. 内置类型常用字母书写规范

undefined

尽量避免使用undefined,除非编译时类型为*。使用undefined的同时也肯定要用到* int 与uint

声明为整数的变量千万别赋值为小数 Do this: 2

Not this: 2.

16进制数字中只许使用小写的x与大写的A-F Do this: 0xFEDCBA Not this: 0Xfedcba

RGB颜色值一律采用6位的16进制数字 Do this:

private const BLACK:uint = 0x000000; Not this:

private const BLACK:uint = 0;

处理脚标的时候,用-1代表不存在这个脚标 Number

尽量用小数点来表示一个number值

7

Do this:

alphaFrom = 0.0; alphaTo = 1.0; Not this:

alphaFrom = 0; alphaTo = 1;

但是不要用小数赋值给一个像素点坐标,虽然原则上这样做是合法的,在实际运行中,此类的小数是会被转换为整数的 Do this:

var xOffset:Number = 3; Not this:

var xOffset:Number = 3.0;

指数中请用小写的e而不要用大写的E Do this: 1.0e12 Not this: 1.0E12

Number值一般要赋一个默认值NaN而不要声明以后不赋初始值 String

Use quotation marks (double quotes), not apostrophes (single quotes), to delimit strings, even if that string contains a quotation mark as a character. 字符串要用双引号而非单引号来标记 Do this:

\"What's up, \\\"Big Boy\\\"?\" Not this:

'What\\'s up, \"Big Boy\"?'

制定特定编码的时候(unicode),使用\p":{"h":15.839,"w":16.229,"x":397.515,"y":803.698,"z":54},"ps":null,"t":"word而不要使用\\U Array

初始化数组的时候尽量使用[],而非new Array() Do this: []

Not this: new Array() And this: [ 1, 2, 3 ] Not this:

new Array(1, 2, 3)

使用new Array(3)的时候,意思是[undefined,undefined,undefined]而不是[3] Object

8

使用{}代替new Object()

使用o = {a:1,b:2}代替o = new Object(); o.a = 1; o.b = 2; o.c = 3; Do this: {}

Not this: new Object() And this:

o = { a: 1, b: 2, c: 3 }; Not this:

o = new Object(); o.a = 1; o.b = 2; o.c = 3; Or this: o = {};

o.a = 1; o.b = 2; o.c = 3;

Function literals

避免使用嵌套函数,尽量使用类与包成员的函数 如果你必须使用嵌套函数,那也必须声明函数返回值,并且在函数的最后一行用分号结束函数块 Do this:

function(i:int):void { doIt(i - 1); doIt(i + 1); } Not this:

function(i:int) { doIt(i - 1); doIt(i + 1) } RegExp literals

尽量直接使用正则表达式,而非创建正则表达式的类 Do this:

var pattern:RegExp = /\\d+/g; Not this:

var pattern:RegExp = new RegExp(\"\\\\d+\XML 与 XMLList

不要用XML的构造函数,直接用XML的表达式就可以了 Do this:

var node:XML = ; Not this:

var node:XML = new XML(\"\"); XML里面的节点值不要用单引号,而用双引号

9

Do this:

var node:XML = ; Not this:

var node:XML = ; Class

导入类的时候用全路径,声明类的时候就不用写全路径了,直接声明类。 Do this:

import mx.controls.Button; ...

var b:Button = new Button(); Not this:

import mx.controls.Button; ...

var b:Button = new mx.controls.Button();

除非有两个名字一样但是包路径不一样的类的时候,这个时候请用全路径的初始化类 import mx.controls.Button; import my.controls.Button; ...

var b:Button = new mx.controls.Button();

4. 表达式

圆括号

圆括号会使得表达式中的运算符看起来很复杂繁琐,所以只在必要的时候使用圆括号。Do this:

var e:Number = a * b / (c + d); Not this:

var e:Number = (a * b) / (c + d); And this:

var e:Boolean = a && b || c == d; Not this:

var e:Boolean = ((a && b) || (c == d)); 强制类型转型

10

不要用一个布尔值与true或者false相比较。 Do this: if (flag) Not this:

if (flag == true) Do this:

var flag:Boolean = a && b; Not this:

var flag:Boolean = (a && b) != false;

不要直接把Number,String,XML,XMLList,Array,Object,或者*强制转型为布尔值,因为这些类型有太多值了,你几乎很难记住哪些值会被强制转型到false Type Number String 对应的会被转到false的值 0, NaN null, \"\" XML / XMLList null, Array Object * null, [] null, {}, 0, NaN, false, undefined, null, {}, 0, NaN, false, Do this:

if (s != null && s != \"\") Not this: if (s)

对于继承自Object的子类,则可以直接转型到布尔值,因为它们只有一个值—null会被转为false,其他值都会被转成true Do this: if (child) Not this:

if (child != null) And this: if (!child) Not this:

if (child == null)

对于int与uint型,只有0会被转成false,其他值都会转成true,所以你想直接强制转换也是可以的,但是不如直接跟0比较来的直观。

尽量使用Foo()的方式来强制转型,少用as操作符。除非在强制转型失败的时候你想得

11

到一个null值,而不是抛出异常的时候,你才可以用as Do this:

IUIComponent(child).document Not this:

(child as IUIComponent).document 值比较

比较两个值的时候,表达式一般写在前面,比如: Do this:

if (n == 3) // \"if n is 3\" Not this:

if (3 == n) // \"if 3 is n\" ++与—操作符

如果使用前置++与后置++效果都差不多,那就选择后置的++,前置的++只有在你确定要用的时候才用 Do this:

for (var i:int = 0; i < n; i++) Not this:

for (var i:int = 0; i < n; ++i) 三元操作符

Use a ternary operator in place of a simple if/else statement, especially for null checks: 在一些检查null的操作时,尽量用三元操作符?:代替简单的if/else Do this:

return item ? item.label : null; Not this: if (!item)

return null; return item.label;

但是不要用多层嵌套的三元操作符 Do this: if (a < b)

return -1; else if (a > b) return 1; return 0; Not this:

12

return a < b ? -1 : (a > b ? 1 : 0); new操作符

在使用new操作符的时候,就算构造函数里面没有参数,也请使用圆括号括起来 Do this:

var b:Button = new Button(); Not this:

var b:Button = new Button;

5. 语句

每个语句结束的时候,请用分号来结束 Do this: a = 1; b = 2; c = 3; Not this: a = 1 b = 2 c = 3 include语句

Use include, not the deprecated #include. Terminate the include statement with a semicolon, like any other statement.

请直接使用include而不要用#include,每个include语句都应该像其他语句一样以分号结束。路径统一用相对路径,不要用绝对路径 Do this:

include \"../core/ComponentVersion.as\"; Not this:

#include \"../core/ComponentVersion.as\" import语句

导入的时候必须导入到具体的类,而不要用导入*的方式 Do this:

import mx.controls.Button; import flash.utils.getTimer; Not this:

import mx.core.*;

13

use namespace语句

不要用::语法来给引用非公开的命名空间,用use语句来代替 Do this:

import mx.core.mx_internal; use namespace mx_internal;

// Later, in some method... doSomething(); Not this:

import mx.core.mx_internal;

// Later, in some method... mx_internal::doSomething(); if 语句

如果一个if后面只跟了一个语句,那就不要用{}的语句块了,直接用单句就可以了Do this: if (flag)

doThing1(); Not this: if (flag) {

doThing1(); }

And this: if (flag)

doThing1(); else

doThing2(): Not this: if (flag) {

doThing1(); } else {

doThing2(); }

如果if/else语句的分支里面有多个语句,那就都用语句块

14

Do this:' if (flag) {

doThing1(); } else {

doThing2(); doThing3(); }

Not this: if (flag)

doThing1(); else {

doThing2(); doThing3(); }

用于多个条件的时候,使用if列表来检测返回false的方式。如果用检测true的方式的话,可能会使用多层嵌套if语句,并且检测结果也要在最后才能出来,如果使用检测false的方式,则可能要快很多。比如: Do this:

if (!condition1) return false; ...

if (!condition2) return false; ...

if (!condition2) return false; ...

return true; Not this:

if (condition1) {

...

if (condition2) {

...

15

if (condition3) {

...

return true; } } }

return false; for循环语句

就算只有单条语句,在使用for循环的时候也要用{}包含起来 Do this:

for (var i:int = 0; i < 3; i++) {

doSomething(i); }

Not this:

for (var i:int = 0; i < 3; i++) doSomething(i);

使用一个变量才存储for循环的临界值,这样可以避免每次循环都去取那个临界值。 Do this:

var n:int = a.length;

for (var i:int = 0; i < n; i++) {

... }

Not this:

for (var i:int = 0; i < a.length; i++) {

... }

如果在for循环里面用到的变量在其他地方没有引用,那直接声明在for循环里面就可以了。 Do this:

for (var i:int = 0; i < 3; i++) Not this: var i:int;

for (i = 0; i < 3; i++) { ...

16

}

while语句

就算只有单条语句,使用while的时候也要用{}括起来 Do this: while (i < n) {

doSomething(i); }

Not this: while (i < n)

doSomething(i); do语句

就算只有单条语句,使用do循环的时候也要用{}括起来 Do this: do {

doSomething(i); }

while (i < n); Not this: do

doSomething(i); while (i < n); switch语句

在每个case分支里面都要用{}括起来,break与return要放在括号里面,而不要放在{}后面。不要把break再加在return后面了。Defualt与其他case块并没有什么不同,所有case块都要用break或者return结束 Do this: switch (n) {

case 0: {

foo(); break; }

case 1:

17

{

bar(); return; }

case 2: {

baz(); return; }

default: {

blech(); break; } }

Not this: switch (n) {

case 0: foo(); break;

case 1: {

bar(); }

break;

case 2: baz(); return; break;

default:

blech(); }

return语句

18

在return后面不要使用多余的圆括号 Do this: return n + 1; Not this:

return (n + 1);

6. 声明

在一个语句里面只声明一个变量或常量。 Do this: var a:int = 1; var b:int = 2; Not this:

var a:int = 1, b:int = 2; override关键字的使用

Override应该放在所有关键字的最前面 Do this:

override protected method measure():void Not this:

protected override method measure():void 访问控制符

一般情况下都要用显式的访问控制福来控制访问,如果没加访问控制符,默认的访问控制符是internal

你要考虑清楚,在你决定将一个方法标记为public或者protected的时候是否真的有必要。Public与protected方法必须加注释说明。 static关键字

Static关键字一律加在访问控制符后面 Do this:

public static const MOVE:String = "move" Not this:

static public const MOVE:String = "move"; final关键字

Final关键字一律加在访问控制符后面 Do this:

public final class BoxDirection

19

Not this:

final public class BoxDirection

所有枚举性质的类都要标记为final类型

所有基本属性与方法(以$开始的)也要标记为final 常量

所有常量都必须是静态类型。 Do this:

public static const ALL:String = \"all\"; Not this:

public const ALL:String = \"all\"; 变量

如果需要给一个变量赋予一个初始值,在声明的时候就直接赋值,而不要等到构造函数的时候再赋值 Do this:

private var counter:int = 1; Not this:

private var counter:int; ...

public function MyClass() {

super(); ...

counter = 1; } 局部变量

局部变量都应该在用到的时候再声明,不要在函数一开始就声明一堆变量 Do this:

private function f(i:int, j:int):int {

var a:int = g(i - 1) + g(i + 1); var b:int = g(a - 1) + g(a + 1); var c:int = g(b - 1) + g(b + 1);

return (a * b * c) / (a + b + c); }

Not this:

private function f(i:int, j:int):int

20

{

var a:int; var b:int; var c:int;

a = g(i - 1) + g(i + 1); b = g(a - 1) + g(a + 1); c = g(b - 1) + g(b + 1);

return (a * b * c) / (a + b + c); }

一个变量名在一个函数内只能用一次,AS3不存在“语句块范围“的变量Do this: var a:int; if (flag) {

a = 1; ... } else {

a = 2; ... }

Not this: if (flag) {

var a:int = 1; ... } else {

var a:int = 2; ... }

And this: var i:int;

for (i = 0; i < n; i++) {

21

... }

for (i = 0; i < n; i++) {

... }

Not this:

for (var i:int = 0; i < n; i++) {

... }

for (var i:int = 0; i < n; i++) {

... } 类

如果一个类只简单的继承自Object,extends Object就可以省略不写了 像loadResources()之类的独立方法,应该标记为static类型 构造函数

如果一个类拥有实例化成员,必须要有构造函数,并且在构造函数里面显式地调用super(),就算super()方法是一个空方法。

如果构造器里面的参数有用到拥有setter方法的参数,那构造器里面的参数应该跟setter方法名一致 Do this:

public function MyClass(foo:int, bar:int) {

super();

this.foo = foo; this.bar = bar; }

Not this:

public function MyClass(fooVal:int, barVal:int) {

super();

foo = fooVal;

22

bar = barVal; }

不要在构造器里面给属性设置初始值,而应该在声明属性的时候就赋一个初始值。当然如果你想在子类里面给这个参数赋值一个不同的初始值的时候,就可以这么把他的赋值放在构造函数里面

格式化 1 行宽

最好不要超过80个字的行宽。

2 缩进

用tab键来进行缩进排版

3 声明

常量,变量,方法的声明都使用单独的行

4 数组脚标

数组脚标左边括号后面或者右括号前面不要插入任何符号 Do this: a[0] Not this: a[ 0 ]

5 逗号

逗号后面都应该紧跟一个空格符,比如参数列表,数组列表,或者对象属性之间的逗号

23

6 数组

数组的左边括号后,右括号前都要用空格符,并且每个逗号后面都应该有空格符 Do this: [ 1, 2, 3 ] Not these: [1, 2, 3] [1,2,3]

An empty array is a special case. 空数组有点特殊: Do this: []

Not this: [ ]

长数组用多行来格式化

static var numberNames:Array /* of String */ = [

\"zero\ \"one\ \"two\ \"three\ \"four\ \"five\ \"six\ \"seven\ \"eight\ \"nine\" ];

7 Object对象

Object的左边括号后,右括号前都要用空格符,并且每个逗号后面都应该有空格符,每个值的前面都要添加空格 Do this:

{ a: 1, b: 2, c: 3 } Not these:

{a: 1, b: 2, c: 3}

24

{a:1, b:2, c:3}

{a:1,b:2,c:3}

An empty Object is a special case. 空Object对象除外 Do this: {}

Not this: { }

Format lengthy object initializers requiring multiple lines with aligned braces: 长Object对象用多行来格式化

private static var TextStyleMap:Object = {

color: true,

fontFamily: true, fontSize: true, fontStyle: true, fontWeight: true, leading: true, marginLeft: true, marginRight: true, textAlign: true,

textDecoration: true, textIndent: true };

8 类型声明

声明的冒号前后不要插入任何符号 Do this:

var n:Number; Not these:

var n : Number;

var n: Number; And this:

function f(n:Number):void

25

Not these:

function f(n : Number) : void function f(n: Number): void

9 操作符

赋值操作符左右用空格分开 Do this: a = 1; Not this: a=1;

连接操作符左右用空格分开 Do this: a + b * c Not this: a+b*c

比较操作符左右用空格分开 Do this: a == b Not this: a==b

前置操作符左右不要用空格分开 Do this: !o

Not this: ! o

后置操作符左右不要用空格分开 Do this: i++

Not this: i ++

10 语句

每个语句单独一行,这样可以便于你断点调试Do this: a = 1;

26

b = 2; c = 3; Not this:

a = 1; b = 2; c = 3;

Align the braces of statement blocks. 花括号要单独一行,正确缩进 Do this:

function f():void {

var n:int = numChildren; for (var i:int = 0; i < n; i++) {

if () {

x = horizontalGap * i; y = verticalGap * i; } } }

Not this:

function f():void {

var n:int = numChildren; for (var i:int = 0; i < n; i++) { if () {

x = horizontalGap * i; y = verticalGap * i; } } }

注释

在编码过程中,尽量使用蓝色的注释,比如方法的注释,参数的注释等。/**

* @private

* The backing variable for the property. */

27

private var _someProp:Foo; /**

* Place all comments for the property with the getter which is defined first. * Comments should cover both get and set behavior as appropriate. */

public function get someProp():Foo { ... } /**

* @private */

public function set someProp(value:Foo):void { ... }

当然如果你只想说明但行语句的特殊意思,也可以使用//来注释

28

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- yrrf.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务