Function package explanation

xiaoxiao2021-03-06  86

[Participate in detailed discussion] Function package explains dingding let me talk about what is the function package ?? I can't fully explain it ... I can only understand myself ^^ In fact, the literal explanation is to seal up Ok, it is true that this is true like this, such as a function, function myfunc (a, b) {Return A B;} Obviously MyFunc is used to return two numbers and then myvar = myfunc (1, 2); myvar Is equal to 3. This is a package, because when you have to get two numbers, you only call myfunc (); then pass the two numbers you have to add, how is the specific myfunc? Add, you don't have to know. This will reach the role of the package. Of course, no one will do it, so it is obviously a bit of a bit of prejudice ^^ "If you let you calculate the circuit composed of two resistors, the total resistance is How much ?? ok, if your electrical is very good, I don't want to know, but your electrical is the same as me, not very, or bad. We still use someone else to package good functions Function Resistance (R1, R2) {IF ((R1 R2) == 0) Trace ("Error: Division cannot be '' '' '' '' '); ELSE RETURN R1 * R2 / (R1 R2);} r = resistance (5, 10); This, don't need to manage what the important electrical formula, as long as you call RESISTANCE (); and pass the two resistance values ​​you know into it, you get it ^^ How, the package is not very well understood, the image is: You pass a set of data into a black box, and another group of data obtained after this black box, the black box is a function of the encapsulated function and this black box The entrance is the parameters of the function .... You just know what the entrance is, how do you make the black box, how is the structure? Ok, say it is almost, don't understand, I will ask. It can only be used to express this content: D Dream8285 package is roughly useful, after writing a function, you can use this function in the future, as long as you know the interface, you don't need to know how the function is doing, for example, you will Will not use gotoandPlay ();? Yes, he is a MM company packaged function but you don't know how to do it, let's talk about the concept of the interface, or use gotoandplay (); and you will tell How do I use it? Oh, you will tell me that gotoandplay (1) So the 1 is the parameter of this function. Okth, in addition to this parameter, do you know how do you have a number of parameters? You will answer me such as gotoa NDPLAY ("XX Frame") GotoandPlay ("XX Scene") is because of these parameters, this is the external interface. We only know that GotoandPlay (10) Time axes jump 10 frames to play, and gotoandplay () This function is in the end How to work, we don't need to know, just know what it provides our interface, then we can do what we can do through this interface, for example, you wrote a function Function MoveToright (mc) {mc._x ;} MoveToright Is the package function, the MC is the external interface. You can just get it, as long as you pass a MC parameter, you can move the MoveToright is a function name MC is the parameter. We will give an example to say that the package function and the difference between the encapsulating function With the benefits of function reset () {_root.mc._alpha = 50;} reset () // or mc.onternalframe = MoveToright; Is it good to achieve the effect of the ALPHA of the MC ?? Is this method? Maybe you will say, there is nothing bad, first stated that a function is then called this function.

The idea is very clear! But is it really feeling very good ?? Is there a bit bad place ?? Oh, in fact, it is not good? Imagine if your MC's name suddenly changed, changed to mymc, you still have to achieve the effect you want, what should you do to find this function, change the MC to MYMC to change 2 places first: The second: MC's naming and then you suddenly discovered that the other 5.6 MCs in the scene also achieved the effect, how to do it? Is it repeated in the function _root.mc1._alpah = 50; _root.mc2._aplha = 50; ... isn't it a stupid method? If you are smart, OK, you will think of establishing a loop for in the function (var i = 0; i <5; i ) {...} huhh but when you don't stop there, have 6, is it Also changed? To the end, your scene is a bit a little change, you have to lubricate the reset function, and repeatedly modify it, this function is in other files, there is no use everywhere, but also For other source files, it is changed to the end to the transparency of some of the MCs in the scene to 50. If you have a constant modification, if you admit it, it is very stupid, then you have to learn to package the function. The function is the simplest. Method, you create a parameter for the function (note: here MC is the parameter, not the reference to instance in the scene) Function Reset (mc) {mc._alpha = 50;}, where you want MC transparent 50, as long as It's better to pass the name of this MC to the parameter. For example, there are 6 MCs in your scene. The name is MC1 ~ MC6; then RESET (MC1); RESET (MC2); reset (mc3); reset (mc4); reset (mc5); reset (MC6); even if you have 600, you don't have to be afraid, I will fix the for (VAR i = 1; i <= 600; i ) {reset (this ["MC" i]) This function is alive. Even if you have ended this project, in the next project, you still have to use some of the MC's transparent settings to 50, as long as this function is OK, this function is the so-called package. The function, the parameter MC, that is, your external interface doesn't matter how MC in your scene, from the head to the end, you don't need to change the content inside the function ------------ Package concept And the use of the package, and the benefits of the package, everyone understands, huh, I haven't understood it, then let's take an abstract point. For example, you have to move, build a moving function Function moving () {Sofa. Location = Rely on the wall; bed. Position = corner; table. Location = Top window;} When you have a chair, are you going to find a moving function, then add a chair. Position = Distance ??? (Casual), when you feel that when you think that the location of the table is not right, you have to change the table. Where is the location =?? Do you have too much trouble, if you are a decoration brigade, each household If the furnishings of people are likely to modify your moving function every time, it turns into the original clumsy approach. If you renovate 50 households, then when one of them have a problem, you want you to redesign You have to go to find, corresponding to its moving function to modify but Is ... Function moving (furniture, location) {furniture. Location = location;} Don't solve the problem to move (table, pending window); moving (bed, corner); moving (chair, relying on the door); .... In fact, the meaning of packaging is to hide the things that do not change. The advantage of exposing the changed things can be made, the program is durable, multi-purpose, and maintains convenience, etc. The above moving function, no matter where you move 50 households, 100 Households, use that one function, it will be done, is it convenient for you to be a designer of the decorative company, such a program must be a big hand praise ... I haven't mention it, I know it, I have been fried squid. Hehe, you have designed the plan, of course, some people need to be moved to move (the table, the window); moving (bed, corner); moving (chair, relying on the door); .... I said the last statement here. "

转载请注明原文地址:https://www.9cbs.com/read-105928.html

New Post(0)