View comments | RSS feed

8.3 Function objects

Global and nested functions can be used as constructors in instantiation expressions, as shown in the following example:

function A() { this.x = 10 }
var o = new A
trace(o.x)     // traces 10

Function objects have a property named prototype whose value is used to initialize the intrinsic delegate property of the objects it creates. The prototype property has a default value of a new instance of the class Object. Building on the previous example:

function A() { this.x = 10 }
function B() {}
B.prototype = new A
var o = new B
trace(o.x)     // traces 10

The value of o is an instance of B, which delegates to an instance of A, which has a property named x with value of 10.

Constructor methods inside of a class are also used to create objects. But, unlike constructor functions, constructor methods create objects with a set of fixed properties (traits) associated with its class and a delegate that is also an instance of its class.

class A {
    var x
    function A() { this.x = 10 }
}
var o = new A
trace(o.x)     // traces 10

There are some subtle differences between the preceding example and the one involving a function constructor:

Class methods are functions that are defined with the static attribute inside of a class definition. A class method cannot be used as a constructor and does not define the this reference. Class methods are in the scope of the class object in which they are defined.

Instance methods are functions that are defined without the static attribute and inside a class definition. Instance methods are associated with an instance of the class in which they are defined. Instance methods can override or implement inherited class or interface methods and always have a value bound to this.

The value of this in an instance method is the value of the instance the method belongs to. When an instance method is extracted from an object, a bound method is created to bind the value of this to that host object. Assignment of the bound method to a property of another object does not affect the binding of this. For example:

class A {
    var x
    function A() { this.x = 10 }
    function m() { trace(this.x) }
}
var a = new A()
var o = { x : 20 }
o.m = a.m
o.m()   // traces 10

Comments


Joe@emeraldforest said on Feb 22, 2008 at 2:34 PM :
"Class methods are functions that are defined with the static attribute inside of a class definition. "

Is this what is called in other places a 'Trait'
http://livedocs.adobe.com/specs/actionscript/3/wwhelp/wwhimpl/js/html/wwhelp.htm?href=as3_specification.html
"The 'static' attribute may only be used inside a class definition and causes the function to become a 'trait' of the class object rather than the instance object."

I am unclear as to the distinction between 'Trait' and 'static method' or 'class method'. Is it a 'trait' if it is a static method of an 'Object' object?
Francis Cheng said on Feb 26, 2008 at 5:03 PM :
A class contains two traits objects, one for the class as a whole and another for the instances of the class. Static methods are stored in the former and instance methods are stored in the latter.

This specification uses the term "trait" to mean a property or method that is fixed rather than dynamic. In other words, any method or variable that you define in your class definition, be it static or not, is a fixed property (aka trait). Notably, this terminology is not used in the ECMAScript 4th edition specifiction. The ECMAScript working group chose to use the term "fixture" instead.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/specs/actionscript/3/as3_specification68.html