flash/As3.0 | 2007/01/26 17:14
<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
bonfa 2007/02/24 08:26 ReplyDelete
We trying to find some more information on the subject by googling for "bound methods" and found the direct quote from the documentation. It is a shame.
BlogIcon 기정e 2007/03/02 13:46 Delete
This blog is the personal space for me . In other words ,not for you. whether I directly had quoted from the documentation or not, it's no problem to have disclosed the source in Korean language.
I am regrettable to your comment.
dk 2007/12/27 03:13 ReplyDelete
That's totally stealing! It dosen't matter whether it's for personal or not. because everyone can read it without knowing where it came from. You should be very shame for this. Make brief explanation of it and refer where did you quote it from.
I'm very shame for what you did and what you replied to bonfa. as the same Korean, also same programmer. Very sad. You should stop doing this.

By the way, you can do the excatly same thing using callback in as2. And usually it is known as a closure like it said in that book, Actually before I read moock's book, I've never heard of this term. Closure method is more common I guess.
BlogIcon 기정e 2007/12/28 07:56 Delete
you are right.
i should have exactly explained where i quote this documentation from. it's my fault.
i hope everyone don't confuse about this quoation.

and i also don't know what function closure is, before reading mook's Essential Actionscript 3.0. but i dont understand this terms exactly yet.
i wonder if you can explain the difference Function closure and Closure method you mentioned.

Name 
Password 
Homepage 
  secret
Comment 
  글쓰기


[PREV] [1] ... [26][27][28][29][30][31][32][33][34] ... [105] [NEXT]

 
전체 (105)
flash (74)
math&physics (4)
programming (11)
Flex2 (1)
Mac (2)
photo (0)
project (6)
주저리주저리 (3)
유용한 자료들 (1)
diary (0)
Book (1)
web (2)
«   2009/01   »
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31