This is an automated email from the ASF dual-hosted git repository.
harbs pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/royale-docs.git
The following commit(s) were added to refs/heads/master by this push:
new 32973f0 More class and interface info
32973f0 is described below
commit 32973f06451e87a63ca230558c8913fb082e2a64
Author: Harbs <[email protected]>
AuthorDate: Sun Dec 19 13:05:37 2021 +0200
More class and interface info
---
features/as3/classes-and-functions.md | 6 ++++--
features/as3/interfaces.md | 24 +++++++++++++++++++++++-
2 files changed, 27 insertions(+), 3 deletions(-)
diff --git a/features/as3/classes-and-functions.md
b/features/as3/classes-and-functions.md
index 43bc712..4c3860c 100644
--- a/features/as3/classes-and-functions.md
+++ b/features/as3/classes-and-functions.md
@@ -28,7 +28,7 @@ Classes and functions in ActionScript 3
As mentioned in [packages](features/as3/packages), each file in ActionScript
needs a `package` declaration. Similarly, there must be exactly one file for
each externally visible Class (or function). Additionally, the class name must
match the class name exactly. By convention, class names start with an
uppercase character.
## Inheritance and interfaces
-ActionScript classes can only inherit from a single `super` class, but you can
declare multiple interfaces. So if you need a class to be more than one
unrelated types, you should use interfaces to declare your types rather than
classes. Sub-classing and declaring interfaces looks like this:
+ActionScript classes can only inherit from a single `super` class, but you can
declare multiple interfaces. So if you need a class to be more than one
unrelated types, you should use [interfaces](features/as3/interfaces) to
declare your types rather than classes. Interfaces is very often a better
design choice that classes for types either way. Sub-classing and declaring
interfaces looks like this:
```
public class SubClass extends SuperClass implements IFoo, IBaz, IBar
@@ -61,7 +61,9 @@ Using `this` is usually optional in ActionScript. If there is
no local variable
Similarly, static accessors and methods can be referenced without using the
class name, so `baz()` will be resolved to `Foo.baz()` automatically. If you
are using a public static method (or accessor) outside the class that you will
need to explicitly call `Foo.baz()`.
## Using `this` inside functions
-A common problem in Javascript is that `this` is lost within functions.
ActionScript does not have this issue. You can freely use `this` inside both
instance methods and nested anonymous functions. The compiler will
automatically wrap the function to resolve `this` at runtime.
+A common problem in Javascript is that `this` is lost within functions.
ActionScript does not have this issue. You can freely use `this` inside
instance methods. If the function is used as a callback, the compiler will
automatically wrap the function to resolve `this` at runtime.
+
+In Royale, it's generally not recommended to use `this` inside non-instance
methods and functions. In fact that compiler will warn you by default if you
do. Generally, the only case where it's necessary to use `this` inside a
function in Royale is if you need `Object.defineProperty` for some reason, and
even that should be very rare. The `org.apache.royale.utils.object` package has
a number of utility functions for helping to define properties without using
`this`. Namely: `defineGetter` [...]
## Package level functions
You don't need to declare a class to use code. You can have "utility"
functions as first class citizens. To create a public function you create a
file similar to a class (but name it camel-case). Assuming your file structure
is like so: `src/com/acme/doAwesome.as` Inside the file you declare the
function like this:
diff --git a/features/as3/interfaces.md b/features/as3/interfaces.md
index 443661f..6267886 100644
--- a/features/as3/interfaces.md
+++ b/features/as3/interfaces.md
@@ -24,4 +24,26 @@ permalink: /features/as3/interfaces
Interfaces in ActionScript 3
-To fill out...
\ No newline at end of file
+## What are interfaces?
+Interfaces is one of the core types in ActionScript. An interface is used to
define a type of an object independent of the implementation. While classes are
where you implement a type, interfaces are where you *declare* the type.
Classes must have function bodies, and interfaces *cannot* have function bodies.
+
+Interfaces *do not* have to declare anything. You can declare an interface
type which does not require any implementation. This is useful if you want to
declare a group of classes belong to a specific type or groups of type. You can
even use use `myInstance is IFoo` at runtime to check types. This type check is
completely implementation independent.
+## What can you declare in interfaces?
+Interfaces do not define *scope*. You don't declare something public. Also,
interfaces are only for *methods*. You cannot declare variables in an
interface. You *can* (and should) declare getter and/or setters in interfaces,
but those must be implemented in your classes as getter and setter functions
and not vars.
+
+Interfaces *must* be implemented as classes. You cannot declare an interface
for a plain Javascript object like you can in
[Typescript](https://www.typescriptlang.org/docs/handbook/2/objects.html).
+
+(Almost) never reference classes in an interface. If the interface needs to
accept or return a type, use an interface. Pretty much the only exception to
this rule is for native types such as `String`, `Number`, `int`, `XML`, etc.
+
+## What does an interface look like?
+
+Here's one example
+```
+package com.acme{
+ public interface IFoo{
+ function get name():String;
+ function set name(name:String):void;
+ function sayFoo(output:IOutput):void;
+ }
+}
+```
\ No newline at end of file