org.as2lib.core.BasicClass +--org.as2lib.util.AccessPermission
AccessPermission
adjusts the access permissions of members like methods
and properties in a specific context.
You can hide methods from for..in loops and protect them from deletion and from being overwritten.
Note that no matter what access permissions you set they can be overwritten.
Also note that the access permissions are not applied to the object but to the reference to the object. That means that the object can for example be enumerable in one reference but not in another.
Example:
var object:Object = new Object();
object.myProperty = new Object();
object.mySecondReference = object.myProperty;
trace("myProperty: Value: " + object.myProperty);
trace("mySecondReference: Value: " + object.mySecondReference);
AccessPermission.set(object, ["myProperty"], AccessPermission.PROTECT_DELETE);
trace("myProperty: Permission: " + AccessPermission.get(object, "myProperty"));
trace("mySecondReference: Permission: " + AccessPermission.get(object, "mySecondReference"));
delete object.myProperty;
delete object.mySecondReference;
trace("myProperty: Value: " + object.myProperty);
trace("mySecondReference: Value: " + object.mySecondReference);
Output:
myProperty: Value: [object Object] mySecondReference: Value: [object Object] myProperty: Permission: 2 mySecondReference: Permission: 0 myProperty: Value: [object Object] mySecondReference: Value: undefined
As you can see, the above statement holds true. We have two references that reference the same object. We set the access permission of one reference. We can then not delete the reference the access permission was applied to, but the other reference.
Following is another example with a property in its normal state and another protected property we applied the ALLOW_NOTHING access permission to.
Example:
var object:Object = new Object();
object.myNormalProperty = "myNormalPropertyValue";
object.myProtectedProperty = "myProtectedPropertyValue";
trace("myNormalProperty: Default Permission: " + AccessPermission.get(object, "myNormalProperty"));
trace("myProtectedProperty: Default Permission: " + AccessPermission.get(object, "myProtectedProperty"));
AccessPermission.set(object, ["myProtectedProperty"], AccessPermission.ALLOW_NOTHING);
trace("myProtectedProperty: New Permission: " + AccessPermission.get(object, "myProtectedProperty"));
object.myNormalProperty = "newMyNormalPropertyValue";
object.myProtectedProperty = "newMyProtectedPropertyValue";
trace("myNormalProperty: Value After Overwriting: " + object.myNormalProperty);
trace("myProtectedProperty: Value After Overwriting: " + object.myProtectedProperty);
for (var i:String in object) {
trace(i + ": Found In For..In Loop, Value: " + object[i]);
}
delete object.myNormalProperty;
delete object.myProtectedProperty;
trace("myNormalProperty: Value After Deletion: " + object.myNormalProperty);
trace("myProtectedProperty: Value After Deletion: " + object.myProtectedProperty);
Output:
myNormalProperty: Default Permission: 0 myProtectedProperty: Default Permission: 0 myProtectedProperty: New Permission: 7 myNormalProperty: Value After Overwriting: newMyNormalPropertyValue myProtectedProperty: Value After Overwriting: myProtectedPropertyValue myNormalProperty: Found In For..In Loop, Value: newMyNormalPropertyValue myNormalProperty: Value After Deletion: undefined myProtectedProperty: Value After Deletion: myProtectedPropertyValue
As you can see the protected property cannot be deleted, overwritten and is hidden from for..in loops, while the non-protected property can be deleted, can be overwritten and can be enumerated.
Besides the get method you can check up on properties for specific access permissions using the isEnumerable, isDeletable and isOverwritable methods.
static public ALLOW_ALL:Number
static public HIDE:Number
static public PROTECT_DELETE:Number
static public PROTECT_OVERWRITE:Number
static public ALLOW_NOTHING:Number
static public function set(target, referenceNames:Array, access:Number):Void
Sets the access permission of a reference by an access code.
The following access codes are applicable:
HIDE | Hides the reference from for-in loops. |
---|---|
PROTECT_DELETE | Protects the reference from deletion |
PROTECT_OVERWRITE | Protects the reference from overwriting |
ALLOW_ALL | Allows everything to be done with the reference. |
ALLOW_NOTHING | Allows nothing to be done with the reference. |
These access codes can be combined as follows to apply multiple access
permissions.
AccessPermission.PROTECT_DELETE | AccessPermission.PROTECT_OVERWRITE
Note that every new invocation of this method simply overwrites the old access permissions of the reference.
target | the object that holds references to the objects the access permissions shall be applied to |
referenceNames | the names of the references to apply the access permission to |
access | the access permissions to apply |
static public function get(target, referenceName:String):Number
Returns the current access permission of the reference.
The permission is represented by a Number
. This number is a bitwise
combination of the three access specifier HIDE, PROTECT_DELETE
and PROTECT_OVERWRITE. You can find out what the returned access
permission number means using these constants.
target | the target object that holds the reference |
referenceName | the name of the reference to return the access permission for |
a number representing the access permission of the reference
static public function isEnumerable(target, referenceName:String):Boolean
Returns whether the reference is enumerable.
target | the target object that holds the reference |
referenceName | the name of the reference to return whether it is enumerable |
true
if the reference is enumerable else false
static public function isOverwritable(target, referenceName:String):Boolean
Returns whether the reference is overwritable.
target | the target object that holds the reference |
referenceName | the name of the reference to return whether it is overwritable |
true
if the reference is overwritable else false
static public function isDeletable(target, referenceName:String):Boolean
Returns whether the reference is deletable.
target | the target object that holds the reference |
referenceName | the name of the reference to return whether it is deletable |
true
if the reference is deletable else false