Summary of Flash programming habits

Source: Internet
Author: User

This section is written in front of the article to enable developers who have started to get started with actionscript programming to develop good programming and development habits from the very beginning.
1. Follow naming rules

One application Program The naming scheme must be consistent and readable. The main functions or purposes of any object must be clearly indicated by the name. Because ActionScript is a dynamic language, it is best to have a suffix that represents the object type. In general, syntaxes such as noun _ verb and adjective _ noun are the most commonly used naming methods, such:
Video name: my_movie.swf
URL entity: course_list_output
Component or object name: chat_mc
Variable or attribute: Username

The method and variable name should start with a lowercase letter, and the object and object construction method should be capitalized. When naming variables, they are case-sensitive and contain uppercase and lowercase letters and underscores.
The following naming rules are invalid:

_ COUNT = 5; // underlines are not allowed for the first character
5 COUNT = 0; // numbers are not allowed for the first character
Foo/bar = true; // contains invalid characters

In addition, the reserved words used by ActionScript cannot be used to name variables.
ActionScript is based on ecmascript, so we can name it according to ecmascript specifications. For example:
Course_list_output = "foo"; // all lowercase letters. Use underscores to separate strings.
Courselistoutput = "foo"; // case-insensitive Mode
Baseurl =; // constant use all uppercase
Maxcountlimit = 10;
Myobject = function () {}; // Constructor
F = new myobject (); // object
Note: You can also use the flash Code Prompt function.
2. Add comments to your code

Code annotation can make the program clearer and easier to read. There are two types of code annotation methods supported by flash: single-line annotation, which is usually used for variable description.

VaR clicks = 0; // variable for number of Button Clicks
Multi-line comments are usually used for function descriptions and comments of large text segments:
Initialize the clicks variable that keeps track of the number of times
The button has been clicked.
*/Some annotation methods with specific meanings: //: Todo: topic
Indicates the beginning of a topic.
//: Bug: [bugid] topic
Shows where a bug is located.
//: Kludge:
It indicates that the following code is incomplete and may be faulty.
//: Tricky:
Tell developers that the following code has an interaction. Be cautious before modifying the code.

3. Maintain code integrity

In any case, we should try to ensure that all the code is in the same position, so that the code can be searched and debugged more easily. When debugging a program, it is very difficult to locate the code. if most of the Code is concentrated in the same frame, the problem will be better solved. Generally, we place all the code in the first frame and separately at the top layer. If a large amount of code is concentrated in the first frame, remember to mark it with comments and add code instructions at the beginning.
// Video voice chat system
// Fcavpresence component
// Copyright ◎ 2002 Macromedia, Inc. All rights reserved.
// Good: liu21st,
// ------------------------------------------------------------- Add a similar annotation before the independent function module:
// Parameter initialization
File ://---------------------------------------------------------
4. initialize the application

Remember to initialize your application. The init function should be the first function in your application class. If you use object-oriented programming, you should initialize your application in the constructor. This function only initializes variables and objects in the application. Other calls can be driven by events.
The following example shows how to initialize a wire entry.
Function fcavpresenceclass (){
This. INIT ();
Fcavpresenceclass. Prototype. init = function (){
This. Name = (this. _ name = NULL? "_ Default _": This. _ name );
This. prefix = "fcavpresence." + this. Name + ".";

5. Use local variables

All local variables are declared using the keyword VaR to avoid being accessed by global variables. More importantly, variables are not overwritten and program logic is obfuscated. For example, the following code does not use VaR to declare and overwrites other variables.
Counter = 7;
Function looptest ()
Trace (Counter );
For (counter = 0; counter <5; counter ++)
Trace (Counter );
Trace (Counter );
Looptest ();
Trace (Counter );
Output result:

6. Add methods and attributes using the prototype when creating an object

When creating an object, we should use the prototype method to add methods or attributes of the object so that the method or attribute can be accessed by all objects of the object or sub-object. This ensures that each function in the memory has only one copy. As a general rule, do not define methods in constructors. The following is a correct example:
// Best practice for creating an object
Myobject = function ()
Myobject. Prototype. Name = "";
Myobject. Prototype. setname = function (name)
This. Name = Name;
Myobject. Prototype. getname = function ()
Return this. Name;
The following code is not available:
// Less desirable practice for creating an object
Myobject = function ()
This. Name = "";
This. setname = function (name)
This. Name = Name;
This. getname = function ()
Return this. Name;
Using the above method, when an object is created, the object will re-copy every attribute and method, which will increase the system memory overhead.
7. standard naming for getting code prompts

The code prompt is no longer displayed if you need to define a name such as _ MC.
However, you need to use the as2.0 specification to write code, such:
VaR members: array = new array ();
After entering members., flash will display the list of methods and attributes that can be used for array objects.
(The key is members: the Role of array. In fact, when we press the variable name, we will automatically display a list Of all supported objects)
However, we do not need to pay attention to naming rules when using object-oriented programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.