 |
|
 |
|
 |
 |
|
|
|
|
|
|
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 |
|
|
|
|
|
 |
|
|