The programming habit of flash

Source: Internet
Author: User
Tags define array comment tag lowercase naming convention variables readable reserved
Programming This section outlines some of the rules you need to follow when programming and creating applications using ActionScript, and how to make our program code more efficient and more readable, and easier to debug applications. Write this section in front of it, and also to allow developers who begin to interact with ActionScript programming to develop good programming and development habits from the start.
1. Follow naming rules The naming plan for an application must be consistent and readable. The main function or use of any entity must be visible according to the name. Because ActionScript is a dynamically typed language, naming is best to contain suffixes that represent object types. Generally speaking, nouns _ verbs and adjectives _ nouns are the most commonly used nomenclature, such as:
Film Name: my_movie.swf
URL entity: Course_list_output
Component or object name: CHAT_MC
Variable or property: The name of the UserName method and variable should begin with a lowercase letter, and the objects and objects should be constructed in uppercase. Names variables are used in a case-by-case way, and they start with a letter, and can include numbers and underscores.
Some of the following names are illegal: _count = 5;//The first character cannot use an underscore
5count = 0;//First character cannot use number
Foo/bar = true;//contains illegal characters in addition, the reserved words that ActionScript uses cannot be used to name variables.
ActionScript is based on ECMAScript, so we can name them according to the ECMAScript specification. Such as:
Course_list_output = "Foo"; All lowercase, using underscores to split strings
Courselistoutput = "Foo"; The way of case-by-case blending
BaseURL = http://www.flash8.net; Constants use ALL Caps
Maxcountlimit = 10;
MyObject = function () {}; Constructors
f = new MyObject (); Object
Note that a good naming convention can also use the Code hints feature of Flash.
2. Add a comment to your codeUsing code annotations makes the program clearer and easier for us to read. There are two types of code annotation methods that Flash supports: single-line annotations, usually for variable descriptions

var clicks = 0; Variable for number of button clicks multiline Comment, usually for feature descriptions and large text annotations:
/*
Initialize the clicks variable that keeps track of the number of times
The button has been clicked.
* * Some annotated methods with specific meanings://: Todo:topic
Indicate the beginning of a topic
: BUG: [BugID] Topic
Shows the location of a bug
: Kludge:
Indicates that the following code is not perfect and there may be a problem
: Tricky: Tell the developer that the following code has interaction, so be careful before you modify it 3. Maintain the integrity of the CodeIn any case, you should make sure that all the code is in the same location as much as possible, making the code easier to search and debug. When we debug the program, it is very difficult to locate the code, if most of the code is concentrated in the same frame, the problem is better solved. Typically, we put the code in the first frame and put it on top of the list alone. If you have a large number of code in the first frame, remember to distinguish it with a comment tag, and then add a code description at the beginning
Video Voice chat system
Fcavpresence components
Copyright 2002 Macromedia, Inc. All rights reserved.
Perfect: liu21st, Liu21st@126.com
---------------------------------------------------------add a similar annotation in front of the standalone function module:
Parameter initialization
file://---------------------------------------------------------
4. Initializing the applicationRemember to initialize your application, the INIT function should be the first function of your application class, and if you use object-oriented programming, you should enter the line initialization work in the constructor. The function simply initializes variables and objects in the application, and other calls can be driven by events.
The following example shows how to enter a line initialization function Fcavpresenceclass () {
This.init ();
}
FCAVPresenceClass.prototype.init = function () {
THIS.name = (This._name = null?) "_default_": this._name);
This.prefix = "fcavpresence." + THIS.name + ".";
}; 5. Using Local variablesAll local variables are declared using the keyword var to avoid being accessed by global variables and, more importantly, to ensure that the variables are not overwritten and confuse the program logic. For example, the following code does not use VAR to declare, overwriting other variables.
counter = 7;
function Looptest ()
{
Trace (counter);
for (counter = 0; counter < 5; counter++)
{
Trace (counter);
}
}
Trace (counter);
Looptest ();
Trace (counter); The output results are:
7
7
0
1
2
3
4
5
6. Adding methods and properties using prototypes when creating objects When we create an object, we should use a prototype method to add the object's methods or properties so that the method or property can be accessed by all entities of that object or child object. This ensures that only one copy of each function in memory is available. As a general rule, do not define methods in constructors. The following is a good 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 desirable:

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, the entity replicates each property and method when the entity of each object is created, aggravating the memory overhead of the system.
7. Canonical naming method get code hint functionNo longer is it necessary to define a name like _MC to display code hints
However, you need to use the AS2.0 specification to write the code, such as:
1:
var members:array = new Array (); then we're entering members. , Flash displays a list of methods and properties available for the Array object
(The key is the role of Members:array, in fact, we call after the variable name: When the automatic will give the list of all supported objects)
But it's not that we don't need to pay attention to naming conventions when we're 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: info-contact@alibabacloud.com 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.