 |
|
 |
|
 |
 |
|
|
|
|
|
|
| as3.0에서 여래개의 이미지파일을 외부에서 로드하려면 일반적으로 flash 에서 기본적으로 제공하는 Loader 클래스를 반복적으로 사용하여 처리를 해야하는데 . 이를 위해 다수의 파일을 순차적 또는 한꺼번에 로드할 수 있는 좀더 통합적인 리소스 관리가 가능한 시퀀스 로더를 제작해 보았다.
본 클래스는 ASAP를 사용하여 제작한 loader 클래스를 AS3.0 버전으로 마이그레이션(migration)한 것이다. ASAP는 AS2.0 버전으로는 몇개 안되는 flash framework 중에 하나이다. 현재 전체적으로 AS3.0 버전으로 마이그레이션 중인 것 같다. 개인적인 생각으로는 코드자체가 방대하여 사용하기엔 좀 부담스럽지만 각각의 클래스와 클래스간의 연결고리, 이벤트처리와 같은 복합적인 내용들을 참고할 수 있어 유용한 부분이 있을 것이다. (ASAP as3.0 버전 SVN http://asaplibrary.googlecode.com/svn/)
<sample code>
import com.dstrict.ub.utils.loader.*; import com.dstrict.ub.events.*;
var loader:LoaderSequencer = new LoaderSequencer(1); loader.addEventListener(LoaderEvent.START, onLoadStart); loader.addEventListener(LoaderEvent.ALL_LOADED, onAllLoadFinished); loader.addEventListener(LoaderEvent.COMPLETE, onLoadDone); loader.addEventListener(LoaderEvent.PROGRESS, onLoadProgress); loader.addEventListener(LoaderEvent.ERROR, onIOError);
//아래와 같이 load 메서드를 사용하여 다수의 파일을 로더에 등록할 수 있다. // loader.load(fileUrl,fileName) loader.load("image1.jpg","1"); loader.load("image2.jpg","2"); loader.load("image3.jpg","3"); loader.load("image4.jpg","4");
function onLoadStart(evt:LoaderEvent):void{ //LoaderEvent 이벤트 오브젝트 속성인 fileName 으로 load 메서드의 fileName과 동일 trace(evt.toString()+"-------->start : "+evt.fileName); }
function onLoadDone(evt:LoaderEvent):void{ this["container"+evt.fileName].addChild(evt.loader.content); } function onAllLoadFinished(evt:LoaderEvent):void{ trace(evt.toString()+"-------->onAllLoadFinished"); } function onLoadProgress(evt:LoaderEvent):void{ trace(evt.toString()+"-------->progress : "+evt.bytesLoaded +" : "+evt.bytesTotal); } function onIOError(evt:LoaderEvent):void{ trace(evt.toString()+"-------->"+evt.message) }
view source code
download sample files...
|
|
| 이 글의 관련글(트랙백) 주소 :: http://kimkijeung.com/trackback/103 |
|
|
|
|
|
|
|
| flash 를 사용하면서 가장 많이 사용하는 부분은 아마도 트윈 부분일 것이다. 오브젝트의 속성값을 변경하면서 수많은 느낌의 모션을 만들수 있었다. 기존에 포함되어 있는 Tween 클래스는 많은 사용자들이 이런 다양한 트윈을 손쉽게 구현할 수 있게 해준다. 하지만, 생각보다 많은 기능들이 포함되어있어서 덩치도 클뿐더러 구현하는 코드 자체도 길어지게 되었다. 또한 AS3.0에서는 garbage collection 의 동작으로 인해 참조값이 없는 객체에 대해서는 collector 에 의해 메모리 수집의 대상이 되기 때문에 트윈클래스의 인스턴스를 지역변수로 참조하여 사용하게 되면 뜻하지 않게 트윈이 완료되기 이전에 객체가 사라지는 문제가 발생할 수도 있다.. 따라서 이런 문제를 해결하기 위해 지역변수보다 좀더 높은 클래스 단계의 scope 의 참조를 사용하게 되는데 이럴경우 불필요하게 많은 참조 변수들을 클래스가 가지게되여 메모리를 소비하게 된다.
그리고 가장 빈번히 사용하게 되는 트윈의 경우 일일이 이벤트 핸들러 함수를 구현하기가 여간 번거로운게 아니다. 코드자체의 길이도 길어질 뿐만 아니라 가독성 측면에서도 떨어지는게 사실이다.
이런 문제를 조금이나마 해결하고자 이전 버전의 Tween 클래스를 기반으로 AS3.0 으로 컨버팅하였다.
기본적은 틀은 이전에 AS2.0 으로 구현한 로직과 큰 차이는 없다. 참조값을 유지하기 위해 전역속성으로 트윈을 적용하였고 객체를 생성하지 않아도 되기 때문에 한결 코드도 간결해졌다.
***** update(2007.10.30) ******
pause 와 resume 기능을 추가 원하는 오브젝트의 트윈에 적용가능함
pause(), pauseAll() 나 resume(), resumeAll() 를 사용하여 전체또는 부분적인 트윈을 제어할 수 있다.
<TweenEngine class>
package com.dstrict.ub.utils.transitions{
import flash.display.DisplayObject; import flash.events.*; import flash.utils.Dictionary; /** DisplayObject tween class 트윈을 적용할 속성의 갯수에 관계없이 일괄적으로 적용가능 마지막 parameter 로 정해진 형식의 이벤트 오브젝트 적용시 이벤트 시작과 끝을 tracking 할 수 있음. @example code <code> function onTweenStart(param):void{ trace("onTweenStart---->"+param); } function onTweenFinish(param):void{ trace("onTweenFinish---->"+param); } TweenEngine.start(circle,{x:400},Regular.easeOut,30,{onStart:onTweenStart, onStartParams:["circle"],onFinish:onTweenFinish,onFinishParams:["circle"]}); </code> */ public class TweenEngine { private static var _referContainer : Dictionary=new Dictionary(true); /** @param targetObj : DisplayObject , tween 적용할 오브젝트 @param tweenProperty : Object , 속성오브젝트 ex. {x:100,y:100,alpha:1} @param easing : Function , 이징함수 @param duration : int , 지속프레임 @param rest : Obejct , [optional] event object ex. {onStart:onTweenStart,onStartParams:[],onFinish:onTweenFinish,onFinishParams:[]} */ public static function start(targetObj:*,tweenProperty:Object,easing:Function,duration:int,...rest):void{ var time:int=1; var beginning:Array=new Array(); var change:Array=new Array(); var isCreated : Boolean=false; var displayObj : DisplayObject; TweenEngine.stop(targetObj);
if(!(targetObj is DisplayObject)){ displayObj = new Shape(); isCreated=true; }else{ displayObj = targetObj; }
for(var i:* in tweenProperty){ beginning.push(targetObj[i]); change.push(tweenProperty[i]-targetObj[i]); } if(rest.length && rest[0].onStart){ var eventStartObject:Object=new Object(); eventStartObject.onStart=rest[0].onStart; eventStartObject.onStartParams=rest[0].onStartParams; //onStart event eventStartObject.onStart.apply(eventStartObject,eventStartObject.onStartParams); } //Nested function var update:Function=function(){ var objIdx:int=0; for(var i:* in tweenProperty){ targetObj[i]=easing(time,beginning[objIdx],change[objIdx],duration); objIdx++; } time++; if(time>duration){ delete _referContainer[displayObj]; displayObj.removeEventListener(Event.ENTER_FRAME,update); if(isCreated) displayObj=null;
if(rest.length && rest[0].onFinish){ var eventFinishObject:Object=new Object(); eventFinishObject.onFinish=rest[0].onFinish; eventFinishObject.onFinishParams=rest[0].onFinishParams; //onFinish event eventFinishObject.onFinish.apply(eventFinishObject,eventFinishObject.onFinishParams); } } } _referContainer[displayObj] = update; displayObj.addEventListener(Event.ENTER_FRAME,update); }
//Update 2007.10.30 /** * Pause a tweening for a given object. */ public static function pause(targetObj : *) : void { if(targetObj.hasEventListener(Event.ENTER_FRAME)) { targetObj.removeEventListener(Event.ENTER_FRAME, _referContainer[targetObj]); } } /** * Pause all tweenings on the engine. */ public static function pauseAll() : void { for(var item:* in _referContainer) { if(item.hasEventListener(Event.ENTER_FRAME)) { item.removeEventListener(Event.ENTER_FRAME, _referContainer[item]); } } } /** * Resume a tweening from a given object. */ public static function resume(targetObj : *) : void { if(_referContainer[targetObj] != null) { targetObj.addEventListener(Event.ENTER_FRAME, _referContainer[targetObj]); } } /** * Resume all tweenings on the engine. */ public static function resumeAll() : void { for(var item:* in _referContainer) { if(_referContainer[item] != null) { item.addEventListener(Event.ENTER_FRAME, _referContainer[item]); } } } /** * Stop a tweening for a given object */ public static function stop(targetObj : *) : void { if(targetObj.hasEventListener(Event.ENTER_FRAME)) { targetObj.removeEventListener(Event.ENTER_FRAME, _referContainer[targetObj]); delete _referContainer[targetObj]; } } /** * Remove all tweenings from the engine. */ public static function stopAll() : void { for(var item:* in _referContainer) { if(item.hasEventListener(Event.ENTER_FRAME)) { item.removeEventListener(Event.ENTER_FRAME, _referContainer[item]); delete _referContainer[item]; } } }
} }
<example code> import com.dstrict.ub.utils.transitions.*; import fl.motion.easing.*;
TweenEngine.start(circle,{x:400,y:300},Cubic.easeInOut,20,{onStart:onTweenStart,onStartParams:["circle start"], onFinish:onTweenFinish,onFinishParams:["circle finish"]});
//----> 마지막 오브젝트 값은 이벤트 핸들러를 참조하는 값으로 옵션사항이다. 단, 사용시 이벤트 오브젝에서 함수이름을 onStart , onFinish 로 이벤트 파라미터를 onStartParams,onFinishParams 로 키값을 사용해야만 한다.
function onTweenStart(param):void{ trace("onTweenStart---->"+param); } function onTweenFinish(param):void{ trace("onTweenFinish---->"+param); }
//stop tween stage.addEventListener(MouseEvent.CLICK,onStop); function onStop(evt:MouseEvent){ TweenEngine.stop(circle); }
download sample files...
|
|
| 이 글의 관련글(트랙백) 주소 :: http://kimkijeung.com/trackback/101 |
|
|
|
|
|
|
|
| AS3.0 에서 사용되는 stage 와 root 의 개념은 AS2.0 버전과는 차이가 있다.
우선 stage 를 살펴보면 이전 버전에서는 stage 오브젝트는 static class 로서 주로 movie size 나 onResize 이벤트를 처리하는데 사용하였지만 새롭게 바뀐 AS3.0 에서는 플래시 무비에 존재하는 모든 displayObject 를 담을수 있는 최상위 컨테이너 개념이 첨가되었다. 이는 어떻게 생각하면 기존의 _root 와 같이 생각할 수도 있다. 하지만 바뀐 모델에 있어서 stage 속성은 인스턴스 속성이다. 다시말해, 전역적으로 접근할 수 없고 반드시 displayObject 의 인스턴스 속성으로서만 참조가 가능하다는 이야기이다. 또한 DisplayObject 가 스크린상의 timeline 이나 display list 에 등록하지 않는다면 null 값을 가지게 된다. (Document class 와 screen 상에 보여지는 오브젝트는 제외) 이런 점이 stage 속성을 사용하는데 있어 상당히 제약사항으로 작용하게 된다. 만약 DisplayObject 가 아닌 다른 Object 를 사용할때 stage 속성은 정의된 속성이 아니므로 constructor 에 stage 속성값을 parameter 로 넘겨주는 방식으로 사용할 수 밖에 없다.
<non-display object classes >
package { import flash.display.Stage;
public class CustomObject { private var _stage:Stage; public function CustomObject(stage:Stage) { _stage= stage; } } }
root 는 상황에 따라 다를 수 있지만 일반적으로 현재의 flash movie 에서 main timeline 을 참조할 수 있는 DisplayObject 속성이다. 다시말해 상대적인 최상위 DisplayObject 를 참조한다. 이전의 _root 속성은 전역속성으로서 가장 최상의 컨테이너를 참조하였기 때문에 로더를 통해 불러왔을경우 그 갯수에 따라 _root 의 참조값이 변하였다. 하지만 root 속성은 현재의 flash movie 를 기준으로 참조값을 얻어오기 때문에 각 swf 에 해당하는 main timeline 의 참조값이다. 즉, 이전버전에서 _lockroot 를 적용한 것 같이 root 속성을 상대적으로 참조할 수 있다. 모든 displayObject 가 각각 timeline 의 참조값을 가질 수 있다는 의미이다. 하지만 root 도 stage 속성과 마찬가지로 인스턴스 속성이다. 따라서 stage 의 제한사항을 그대로 가지고 있다.
따라서 이런 문제점들을 해결하기 위해서는 위와같이 각각의 참조값을 parameter 로 넘겨주는 방법과 초기화 함수를 실행하여 addChild 이후에 참조할 수 있도록 처리해야한다. 또다른 방법으로는 Document class 를 활용하여 전역속성으로 만드는 것이다. Document class 에서는 Stage 에 자동으로 추가되는 main timeline 으로 인해 생성되자마자 stage 속성과 root 속성을 참조할 수 있다. <static 속성으로 참조할 수 있는 MainStage class>
package { import flash.display.DisplayObject; import flash.display.*; public class MainStage extends MovieClip { public static var stage:Stage; public static var root:DisplayObject; public function MainStage() { MainStage.stage = this.stage; MainStage.root = this.root; } } } <Document class>
package { public class DocumentClass extends MainStage { public function DocumentClass() { } } }
위와 같이 document class 에서 MainStage class 을 상속받아 사용하면 어느 오브젝트에서라도 전역적으로 stage를 참조 할 수 있다. 하지만 이는 전역속성으로서 의미가 있는 stage 에만 유용할 뿐 각기 다른 root 속성을 참조하기에는 문제가 있다. root 속성 참조 대한 문제는 ADD_TO_STAGE 이벤트를 이용하여 오브젝트마다 일일이 체크하는 방법이나 아니면 초기화 함수를 실행하여 처리하는 방법밖에 없는것 같다.(어떤 다른 방법이 있을까?????)
timeline 으로 부터 종속되지 않도록 stage 속성과 root 속성을 추가한것은 참 좋은 방법인것 같다. 하지만 그로 인해 개발자들이 일일이 신경쓰지 않으면 안되는 것들이 정말 많이 생긴것 같다.
timeline 과 class..........플래시의 특성상 종속적일 수 밖에 없는데...... 어떤 것이 더 나은 방법일까?....의구심이 든다.
|
|
| 이 글의 관련글(트랙백) 주소 :: http://kimkijeung.com/trackback/96 |
|
|
|
|
|
|
|
| 가변적인 메소드의 parameter 갯수를 이용하려면 AS2.0 버전까지는 arguments 오브젝트를 사용하여 임의의 parameter 에 접근할 수 있었다. 하지만 AS3.0 에서는 컴파일러의 좀더 철저한 데이타 타입의 체크와 parameter 관리에 의해 기존의 방법을 쓸 수가 없게 되었다.
<AS2.0> function parameterTest():Void{ trace(arguments.length) //parameter 갯수를 알수 있음 }
parameterTest(1,2,3); result--> 3
<AS3.0> function parameterTest():void{ trace(arguments.length) }
parameterTest(1,2,3); result--> compiler error : Incorrect number of arguments.
AS3.0 에서는 메소드 정의시 parameter 갯수와 호출시 parameter 갯수가 일치해야만 한다. 그렇지 않으면 위와 같이 arguments 갯수에 오류가 있다고 에러를 발생시키게 된다. 이를 위해 ...(rest) paremeter 구문을 새롭게 제공하였는데 사용법은 단순히 ... 뒤에 사용자가 원하는 parameter array 이름을 써주면 된다.
function parameterTest(...param):void{ trace(param.length) // param 은 배열 타입이다. }
다른 parameter 와 같이 사용하려면 function parameterTest(str1:String,str2:String,...param):void{ //.....statements } 마지막에 ...(rest) parameter 를 써주면 된다.
주의할 점은 위 방법을 사용하면서 arguments 오브젝트를 동시에 사용할 수 없다는 점이다.
|
|
| 이 글의 관련글(트랙백) 주소 :: http://kimkijeung.com/trackback/95 |
|
|
|
|
|
|
|
오래전에 사놓고 프로젝트 때문에 읽어보지 못한 책들을 이제서야 한두권씩 찾아서 읽기 시작했다. 1년 전까지만해도 왜 플래시로는 design pattern 책이 나오질 않을까?....우리나라는 그렇다 치더라도 외국에는 충분히 나왔어야할 내용의 책인데...하고 생각했던 적이 있다. 물론 이전에도 패턴에 대해 언급했던 책은 있었지만 전문적으로 플래시를 이용하여 적용한 패턴 책은 찾아 볼 수 없었다.
이제 AS3.0 나 발표된지도 어느정도 지나서인지 관련책들이 많이 보이는것 같다. 안타깝게도 해외에서만 말이다. 처음 이책을 발견하고 그자리에서 구매해버렸다. 자그마치 $50 달러....
아직 자세히 살펴보지는 못했지만 내가 기대하고 있던 내용의 책이였다. AS3.0 으로 바뀌면서 문법자체가 자바랑 상당부분 비슷하게 바뀌었기 때문에 문법적으로 자바 디자인 패턴책과 엄청난 차이가 있는것은 아니다. 하지만 플래시만의 고유의 영역에 있어서 자바나 다른 언어와는 다른 구조적인 이유로 인해 발생하는 패턴 적용의 이슈들을 어느정도 다뤄줬기 때문에 상당히 유용할것 같다.
많은 플래시 개발자들이 아직까지 플래시로 무슨 디자인패턴까지 쓰냐고 .... 아직까지 인터페이스의 개념조차도 상당수의 플래시 개발자들에게 생소한 마당에 디자인 패턴이라니.... 하면서 효용성에 있어 의문을 제기한다.
물론 나도 이점에 있어서 어느정도 동의한다. 아직까지 플래시 개발에 있어서 기존 응용프로그램 개발만큼의 큰 규모의 프로젝트가 적었기도 했고 대부분 소규모 웹사이트 개발이 전부였을 것이다. 그래서 그런지 플래시 개발을 여러사람이 공동으로 진행했다라는 소리는 좀처럼 들어보지 못한것 같다. 또한 플래시 툴의 특성상 디자인 베이스의 사용자들이 개발자로 전향했던 경우가 많아서 정통 개발자들의 관심인 여러가지 문제에 있어 플래시 개발자들에겐 논외의 대상일 수도 있다.
그렇지만 앞으로의 actionscript 는 그런 문제에 있어 먼저 개발자들이 인지하고 좀더 구조적인 프로그래밍 스킬을 쌓아가지 않으면 안되는 모양새인것 같다. 해외에서는 이미 상당수의 개발자들이 플래시 오픈소스 프로젝트를 진행하고 있다. 혼자서 하는것이 아닌 여러사람이 공동으로 개발에 참여하고 결과물을 내놓는 것이다. 이들이 이렇게 까지 할수 있는 이유 중에 하나는 바로 의사소통을 할수 있는 도구.....그들 나름대로의 프레임워크가 존재하기 때문이다. 이런 프레임워크 정의는 어느정도 구조적인 개발의 전제하에 진행되어야할 것이다.
플래시....참 어떻게 보면 쉬운 툴이고 어떻게 보면 너무 어려운 툴이다. 요즘들어 적어도 Interaction Designer 에게는 플래시 그 이외의 배워야 할것이 너무 많다는걸 느낀다. 뭔가 새로운걸 만들어야한다는 생각과....계속 발전하는 기술.....새로운 기술을 익혀 적용하기에도 바쁜데 남들과 다른 그 어떤 새로운 것을 만들어야 한다는 생각이 들때면 가끔 슬퍼질때가 있다....
예술은 아는 만큼 보이고 아는만큼 느낄수 있다고 한다. 플래시도 알면 알수록 내가 가야할 길이 너무 멀다라는걸 새삼 느낀다.
Table of Contents:
Part I - Successful Projects 1. How to Design Applications 2. Programming to Interfaces Part II - Patterns 3. MVC 4. Singleton 5. Factory (Abstract Factory and Factory Method) 6. Proxy 7. Iterator 8. Composite 9. Decorator 10. Command 11. Memento 12. State Part III - Advanced ActionScript Topics 13. Working with Events 14. Sending and Loading Data 15. E4X (XML) 16. Regular Expressions
책정보보기
|
|
| 이 글의 관련글(트랙백) 주소 :: http://kimkijeung.com/trackback/88 |
|
|
|
|
|
|
|
| As3.0 에서는 ByteArray Class 의 지원으로 bynary code 를 직접 플래시에서 읽고 쓸수가 있다. 플래시 외부로 data 를 export 할때나 불러들일때 압축된 bynary string 를 사용할 수 있어 기존에 할수 없었던 많은 작업들이 가능해졌다.
그중에 하나가 flash 안으로 zip 파일 형태를 로드할 수 있다는 것이다. 또한 그 스트리밍 데이타를 이용하여 zip 파일내에있는 이미지나 파일이 로드될 때 가져와서 쓸 수 있다. 단, 여기서 소개된 소스는 standard zip 파일 형식만 지원한다.
public function loadZip() { var request:URLRequest = new URLRequest("your.zip"); var zip:FZip = new FZip(); zip.addEventListener(FZipEvent.FILE_LOADED, fileCompleteHandler); zip.load(request); }
private function fileCompleteHandler(evt:FZipEvent):void { var file:FZipFile = evt.file; trace("File loaded: " + file.filename) trace(" " + file.sizeCompressed); trace(" " + file.sizeUncompressed); }
다운로드
관련사이트 (http://codeazur.com.br/lab/fzip/)
|
|
| 이 글의 관련글(트랙백) 주소 :: http://kimkijeung.com/trackback/86 |
|
|
|
|
|
|
|
<These quotes are from the Actionscript 3.0 language reference>
A bound method, sometimes called a method closure, is simply a method that is extracted from its instance. Examples of bound methods include methods that are passed as arguments to a function or returned as values from a function. New in ActionScript 3.0, a bound method is similar to a function closure in that it retains its lexical environment even when extracted from its instance. The key difference, however, between a bound method and a function closure is that the this reference for a bound method remains linked, or bound, to the instance that implements the method. In other words, the this reference in a bound method always points to the original object that implemented the method. For function closures, the this reference is generic, which means that it points to whatever object the function is associated with at the time it is invoked. Understanding bound methods is important if you use the this keyword. Recall that the this keyword provides a reference to a method’s parent object. Most ActionScript programmers expect that the this keyword always refers to the object or class that contains the definition of a method. Without method binding, however, this would not always be true. In previous versions of ActionScript, for example, the this reference did not always refer to the instance that implemented the method. When methods are extracted from an instance in ActionScript 2.0, not only is the this reference not bound to the original instance, but also the member variables and methods of the instance’s class are not available. This is not a problem in ActionScript 3.0 because bound methods are automatically created when you pass a method as a parameter. Bound methods ensure that the this keyword always references the object or class in which a method is defined.
The following code defines a class named ThisTest, which contains a method named foo() that defines the bound method, and a method named bar() that returns the bound method. Code external to the class creates an instance of the ThisTest class, calls the bar() method, and stores the return value in a variable named myFunc.
class ThisTest { private var num:Number = 3; function foo () { // bound method defined trace ("foo's this: " + this); trace ("num: " + num); } function bar () { return foo; // bound method returned } } var myTest:ThisTest = new ThisTest(); var myFunc:Function = myTest.bar(); trace(this); // output: [object global] myFunc();
/* output: foo's this: [object ThisTest] output: num: 3 */
The last two lines of code show that the this reference in the bound method foo() still points to an instance of ThisTest class, even though the this reference in the line just before it points to the global object. Moreover, the bound method stored in the myFunc variable still has access to the member variables of the ThisTest class. If this same code is run in ActionScript 2.0, the this references would match and the num variable would be undefined. <These quotes are from the Actionscript 3.0 language reference>
도큐먼트 문서에서 개인적으로 AS3.0 이 새롭게 지원하는 기능중에 가장 좋았던 것 중에 하나다. 더이상 중첩된 메서드 안에서 클래스 안에 선언된 메서드의 참조값을 알아내기 위해 지역변수로 참조를 하지 않아도 된다. 코드 가독성 측면에 있어서 좋지 않았었는데 이젠 끝까지 메서드 선언된 참조 위치를 가지고 있으니 파라미터나 리턴 값에 의해 참조값이 바뀔 염려는 없어진 셈이다.
|
|
| 이 글의 관련글(트랙백) 주소 :: http://kimkijeung.com/trackback/81 |
|
|
|
|
|
|
|
Methods are defined using the function keyword.
You can use a function statement, such as the following:
public function sampleFunction() : String {}
Or you can use a variable to which you assign a function expression, as follows:
public var sampleFunction:Function = function () : String {}
In most cases you will want to use a function statement instead of a function expression for
the following reasons:
* Function statements are more concise and easier to read.
Function statements 는 좀더 간결하고 가독성이 뛰어나다.
* Function statements allow you to use the override and final keywords.
Function statements 는 override 와 final 키워드를 사용할 수 있게 한다.(override 방법은 expression 방법으로는 불가능합니다.)
* Function statements create a stronger bond between the identifier—that is, the name of the function—and the code within the method body. Because the value of a variable can
be changed with an assignment statement, the connection between a variable and its
function expression can be severed at any time. Although you can work around this issue
by declaring the variable with const instead of var, such a technique is not considered a
best practice because it makes the code hard to read and prevents the use of the override
and final keywords.
Function statements 는 식별자(함수 이름과 메소드 몸체안에 있는 코드) 와의 결합을 더 강하게 한다.
변수값이 할당구문에 의해 변할 수 있기 때문에 function expression 과 변수와의 연결이 언제든지 위험하게 될수 있다.
비록 var 대신에 const 를 사용하여 변수선언을 하여 문제를 해결할 수 있겠지만, 그런 기술은 오버라이드(override)와 final 키워드의 사용을 막고, 코드를 읽기 어렵게 만들기 때문에 좋은 방법이라고 생각되어지지 않는다.
One case in which you must use a function expression is when you choose to attach a function
to the prototype object.
전에는 function statement 방법과 function expression 과의 차이점에 큰 영향을 느끼지 못했다.
주로 가독성 측면에서 좋아서 전자의 방법으로 코딩을 해왔는데 이 방법이 추천하는 방식이였다니....
가독성 측면이나 메소드 오버라이드 같은 기능을 활용하려면 function statement 방법을 사용해야할 것이다.
|
|
| 이 글의 관련글(트랙백) 주소 :: http://kimkijeung.com/trackback/74 |
|
| | |