A function is a code that can be reused in a program. You can pass the value or object you want to handle to the function in the form of a parameter, and then the result is obtained by the function. From another perspective, the purpose of a function is to simplify the burden of programming, reduce the amount of code and improve efficiency. and a well written function, like a "black box", you just know how to call it, and do not have to care about its specific function is how to achieve Create your own function---function definition (definition) To create a function, you need to have a definition of the function. For the Actions cript, there is no return value type, the formal parameter argument and so on things to be discussed. The following is the definition of a simple function: function for calculating rectangular area function Areaofbox (A, b) { return a*b; Return the result here } Test function Area = Areaofbox (3, 6); Trace ("area=" +area); Now let's analyze the structure of the function definition. The function keyword indicates that this is a functional definition, not a piece of code execution. Then the name of the function: Areaofbox. The function name is followed by the function's argument list (or without parameters, but parentheses are required). The following braces are the implementation code for the function, the Actions cript statement. If the function needs to return a value, you can use return keyword plus the variable name, expression, or constant name to be returned. There can be more than one return statement in a function, but whenever any one of the return is executed, the function terminates without further execution. If there is no return statement, the last statement at the end of the function ends after execution. Because of the particularity of the Actions cript, the parameter definition of a function does not require a declaration of the parameter type, that is, the parameter type can not be specified. This saves a lot of trouble and also brings some problems. Although the penultimate line in the previous example was changed to area = Areaofbox ("3", 6); Also can get 18 of the results, but this is very detrimental to the stability of the program (if the function used in the a+b, it will become a string concatenation operation, the result will be error). So, sometimes type checking in a function is not rare. Parametric is used in the function body to represent the object to manipulate. The operation of a parameter variable in a function is the operation of the argument passed to the function. The a*b in the example above will be converted to the actual value of the parameter 3*6 processing when you call the function.
-------------------------------------------------------------------------------- Functions also have a method of creating, called function literal, which is declared not by a formal function declaration but by an unnamed function within an expression: Areaofbox = function (a,b) {return a*b;}; Trace ("Area=" +areaofbox (2,3)); This form of declaration is often used in the method of an object or in the function declaration of a function library. Please note that in the help of Flash MX The example code for this declaration is incorrect in the function definition section.
-------------------------------------------------------------------------------- Above is a function with a return value that uses the return keyword at the end of the function to back the result. function can also not return any values. The following example: function with no return value function Areaofbox (A, b) { _root.area = a*b; } Test function Areaofbox (3, 6); Trace ("area=" +_root.area); The result is also 18, except that the final result is passed to the specified variable area under _root. The following example is simpler: function for calculating rectangular area function Simplefunc () { Trace ("nothing"); Return the result here } Test function Simplefunc (); The final output is the string in trace. That is, a function can have neither parameters nor return values, but a collection of operations. The use of visible functions is highly flexible.
-------------------------------------------------------------------------------- As with variables, functions can also have global. Just add a _global to the front when you declare it: global function for calculating rectangular area _global.areaofbox = function (A, b) { return a*b; Return the result here } Test function Area = Areaofbox (3, 6); Trace ("area=" +area); Note that the keyword, which appears in the body of the function, represents the MovieClip of the calling function, not the MovieClip of the function body. This can easily be overlooked and produce unexpected results. The Function.call () and function.apply () methods are used if you want to specify the object represented by this. In a later introduction to the arguments object. Check for function parameters
In order to ensure the correctness of the function, we sometimes have to check whether the user has given enough or the correct type of parameters. Here's what I've changed for the example above: global function for calculating rectangular area _global.areaofbox = function (A, b) { Outputs the sum of two parameters to understand the following typeof statement Trace ("a+b=" + (a+b)); Detection parameters are sufficient and of the correct type if (a==undefined | | b==undefined | | | typeof (A+B)!= "number") { Trace ("< parameter error >"); return 0; Returns 0 as a result of an error } return a*b; Return the result here } Test function Trace ("----below is the correct parameter----"); Area = Areaofbox (3, 6); Trace ("area=" +area); Trace ("----below is the wrong parameter----"); To save space, I call the function directly in the command, rather than assigning the return value to a variable In fact, this is our common function invocation method Trace ("area=" + areaofbox (3, "6")); Trace ("----below is insufficient parameter----"); Trace ("Area=" +areaofbox (3)); In the example above I use a==undefined to determine whether a is assigned (that is, whether it is defined, see the previous chapter for the content of the undefined data type). To make sure it was foolproof, I used b==undefined to ensure that B was also assigned, using a logical "or" operator in the middle. To connect the two conditions. In addition, after these two conditions I have typeof (A+b)!= "number" to confirm that the parameter type is correct (refer to the previous chapter for information about the TypeOf keyword). Here I take advantage of a feature of the Actions cript: The sum of the numbers and the strings is prioritized as string processing. So as long as there is a string in the A+b, then the return value of the entire a+b is a string, and the result of detection by typeof in the back is naturally not the "number" we want. Through Trace ("a+b=" + (a+b)); The results of the output can see this point.
-------------------------------------------------------------------------------- In the Actions cript, there are predefined builtin functions in addition to user-defined functions. For different objects, there are also different functions, or methods, that can be invoked. The following is a list of the highest priority system-built functions: Function description Boolean converts the given parameter value to a logical value (also called a Boolean value) Escape converts the given parameters to strings and encodes them in URL format (all non-standard characters are converted to hexadecimal values beginning with%) Eval returns the object represented by the given parameter, which can be a constant, an expression, a property, etc. (which is often used in duplicatemovieclip) GetProperty returns the specified property value for the specified object Gettimer returns the number of milliseconds elapsed from the beginning of the animation to the current GetVersion return Flash version and operating system information Isfinite returns whether the given argument is poor (logical return value) Create a new object object_1 = new Object (); Object_1.value = 0; Add the value attribute to the object and give the initial value 0 Object_2 = object_1; A object_2 is copied by object_1, at which point the Value property is both 0 <!--/message--><!--Sig--> Test function Areaofbox.call (Object_1, 3, 6); Trace ("object_1.value=" +object_1.value); Array_ab = [4, 5]; Creating an array of parameters Areaofbox.apply (object_2, Array_ab); Trace ("object_2.value=" +object_2.value);
Why call and apply to invoke a function? Is this not more troublesome than the direct call in front? Sure, it's a hassle to call a function, but did you notice the first argument I used? The first argument allows you to specify what objects the This keyword in the body of the function represents, which is the benefit of call and apply (the contents of the object and this are mentioned in later chapters, not to mention here). This benefit is only in the actual programming you can understand, here is just a brief introduction, let you have a basic concept. If the This keyword is not used in a function, the first argument is substituted with null. In this example, the first return value is placed in the Value property of the object_1, and the other is placed in the Object_2 Value property. By using the first argument of call and apply, we let this in the function body represent the object_1 and object_2 two objects successively. The actual argument after the first argument is given exactly according to the number and type of arguments to invoke the function. As for the difference between call and apply, it is clear that the actual parameters of apply are passed through an array. Arguments objects Arguments objects A arguments object, as its name implies, is a parameter object that contains the parameters of a function call. As an array object, it has only three properties: Attribute description Arguments.callee a function that is being called Arguments.caller the function that is making this call Arguments.length the number of arguments passed to the called function |
Source: Flash Bar |
|