Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

recycleBin.as

Name: _recycleBin: recycle(), restore(), emptyRecycleBin() - delete props/methods with ability to restore
Author: senocular: www.senocular.com
Date: 1899-12-31T00:05:24.700
Documentation:
Object RECYCLE, RESOTORE and EMPTYRECYCLEBIN:
Offers an alternative to the delete keyword.  Instead of deleting
a property or method from an object, you can recycle it which
temporarily deletes it but allows you to again restore it should
you desire to.  All recycled properties/methods are stored in a
_recycleBin object attached to the object which this is used on.
recycle deletes the property/method while restore brings it back.
emptyRecycleBin deletes all recycled props/meths of that object.
 
[recycle],[restore]
Arguments:
- prop: property or method (as a string) of this object you wish to delete, or rather
recycle to or restore from the recycling bin.
 
[restore]
Returns:
- restore returns true or false depending whether or not the property was found in the 
recycleBin.  If the property is found, it is added to the object from the recycleBin
and true is returned.  If the property is not found (either from it never being recycled
in the first place or if emptyRecycleBin is called which deletes all stored recycled
props/meths) then nothing is restored and false is returned.
 
Warning:
- Try to refrain from recycling the _recycleBin ;)

Example:
// on a movieclip which uses a move function to move sideways
// when the mouse is pressed the move function is recycled and
// the clip no longer moves.  Then the mouse is released, the
// move function is restored and movement continues.
onClipEvent(load){
	move = function(){
		_x++;
	}
}
onClipEvent(enterFrame){
	move();
}
onClipEvent(mouseDown){
	this.recycle("move");
}
onClipEvent(mouseUp){
	trace(this.restore("move")); // traces success of restore()
}

1
2
3
4
5
6
7
8
9
10
11
12
13
Object.prototype.recycle = function(prop){
	if (this._recycleBin == undefined) this._recycleBin = {};
	this._recycleBin[prop] = this[prop];
	delete this[prop];
}
Object.prototype.restore = function(prop){
	if (!this._recycleBin[prop]) return false;
	this[prop] = this._recycleBin[prop];
	return true;
}
Object.prototype.emptyRecycleBin = function(){
	this._recycleBin = {};
}