By Zevan | October 28, 2018
This uses a proxy to make sure all keys/props of an object are always defined.
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
| let spec = {
get: (o, key) => {
return o[key] != null ? o[key] : o[key] = O();
},
set: (o, key, v) => {
o[key] = v;
}
};
let O = () => {
return new Proxy({}, spec);
};
let dynamic = O();
dynamic.prop.creation = 'is interesting';
dynamic.prop.stuff.not.clear.what.this.could.be.used.for = 123;
// log out full structure
let f = (o) => {
for (let i in o) {
console.log(o[i]);
if (typeof o[i] === 'object') f(o[i]);
}
};
f(dynamic); |
Outputs:
Proxy {creation: "is interesting", stuff: Proxy}
is interesting
Proxy {not: Proxy}
Proxy {clear: Proxy}
Proxy {what: Proxy}
Proxy {this: Proxy}
Proxy {could: Proxy}
Proxy {be: Proxy}
Proxy {used: Proxy}
Proxy {for: 123}
123
Choose two colors to breed them and create 5 new colors:
This is a speed coded pen from awhile back - the features object is interesting - it allows two objects to be bred together. In this case two colors. I could see this is as part of some advanced/abstract colorpicker that allows the user to home in on a color.
Actionscript:
-
package {
-
-
import flash.display.Sprite;
-
import flash.utils.describeType;
-
-
public class Main extends Sprite {
-
-
public function Main(){
-
var test:Test = new Test();
-
var desc:XML= describeType(test);
-
// public vars
-
for each (var v:XML in desc.variable){
-
trace(v.@name, test[v.@name]);
-
}
-
// getters
-
for each (v in desc.accessor){
-
trace(v.@name, test[v.@name]);
-
}
-
}
-
-
}
-
}
-
-
class Test{
-
public var a:Number = 123;
-
public var b:Number = 100;
-
private var _getterVal:Boolean = false;
-
public function get getter():Boolean{
-
return _getterVal;
-
}
-
}
-
/*
-
outputs:
-
b 100
-
a 123
-
getter false
-
*/
I'm working on a few libraries, QuickBox2D and a library for auto-generated UI stuff... this technique just came in handy. It shows how to use describeType() to loop through public vars and getters of a given class.
The title of this post should really be Loop Through All PUBLIC properties of a class.... but it was long enough as it is....
Note: this should be run as document class
Also posted in OOP, XML, dynamic | Tagged actionscript, flash |
By Zevan | April 11, 2009
Actionscript:
-
var a:Vector.<Sprite> = new Vector.<Sprite>();
-
-
trace("unsorted");
-
for (var i:int = 0; i<10; i++){
-
var s:Sprite = new Sprite();
-
s.x = int(Math.random()*100);
-
a.push(s);
-
trace(s.x);
-
}
-
-
quickSortOn(a, "x", 0, a.length-1);
-
-
trace("sorted");
-
for (i= 0; i<10; i++){
-
trace(a[i].x);
-
}
-
-
// modified code from kirupa.com
-
// http://www.kirupa.com/developer/actionscript/quickSort.htm
-
function quickSortOn(a:Vector.<Sprite>, prop:String, left:int, right:int):void {
-
var i:int = 0, j:int = 0, pivot:Sprite, tmp:Sprite;
-
i=left;
-
j=right;
-
pivot = a[Math.round((left+right)*.5)];
-
while (i<=j) {
-
while (a[i][prop]<pivot[prop]) i++;
-
while (a[j][prop]>pivot[prop]) j--;
-
if (i<=j) {
-
tmp=a[i];
-
a[i]=a[j];
-
i++;
-
a[j]=tmp;
-
j--;
-
}
-
}
-
if (left<j) quickSortOn(a, prop, left, j);
-
if (i<right) quickSortOn(a, prop, i, right);
-
}
-
/* outputs something like:
-
unsorted
-
26
-
33
-
20
-
63
-
7
-
68
-
75
-
39
-
67
-
53
-
sorted
-
7
-
20
-
26
-
33
-
39
-
53
-
63
-
67
-
68
-
75
-
*/
This demo is my first quick stab at using at a sortOn() function for the Vector class. It sorts a Vector of Sprites by their x property.
Recently there were a few times when I was prototyping ideas and suddenly realized that I needed to change my Vector to an Array because I needed to use sortOn().(If you don't already know, there is no built in sortOn() method for the Vector class). In the past I spent some time with sorting algorithms, bubble, insertion etc... so I knew I could pretty easily write my own sortOn(), but I also realized that a generic implementation wouldn't be easy/possible without loosing the type of the Vector. What I mean is, if you have a Vector of Sprites, you need a sorting method that takes a Vector.< Sprite > type as an argument (as seen above), if you have a Vector of TextFields you need a Vector.< TextField > type as an argument. You could of course use a generic type, but this kind of defeats the purpose of using a vector in the first place...
I will likely post a revised version of this in the near future with a slightly improved implementation of QuickSort. I haven't spent that much time with this, but If I recall correctly this is not the ideal implementation. I ported this code from a nice Kirupa tutorial and modified it to sort based on a property...