zQuery AS Weirdness

I was on the train the other day with my laptop and I though – hey, it would be pretty easy to write a hacky piece of code that will add method chaining to any AS object – and it would also be interesting if you could get at composed objects like the graphics class via a method call so that you could function chain those as well – obviously inspired by jQuery… but not really much like it… and significantly less useful (warning this is just an experimental hack):

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
 
// slow
var t = getTimer();
 
// untyped to stress js style
var rect = $(new Sprite)
           .add("click", function(e){ e.target.rotation+=10 })
           .appendTo(this)             
           .rotation(45).x(100).y(100)
           .graphics()
           .beginFill(0)
           .drawRect(-50,-50,100,100);
 
var line = $(new Sprite)
           .prependTo(this)
           .attr({x:30, y:30, rotation : 10})
           .graphics()
           .lineStyle(0, 0xFF0000)
           .lineTo(100,100)
trace(getTimer() - t);
 
t = getTimer();
 
// fast
var line2;
with(line2 = addChild(new Sprite)){
	with(graphics) lineStyle(0,0x0000FF),
	lineTo(100,100);
	rotation = 10, x = 200, y = 30;
}
 
var rect2;
with(rect2 = addChild(new Sprite)){
	with(graphics) beginFill(0xFF0000), 
	drawRect(-50,-50,100,100);
	rotation = 45, x = 300, y = 100;
}
trace(getTimer() - t);
 
// zQuery joke function
function $(object:*):Object{ 
  var i:XML, access:String, nm:String;
  var wrapper:Object = {};
  var structure:XML = describeType(object);
  for each(i in structure.method){
	  nm = i.@name;
	  (function(nm):void{
	    wrapper[nm] = function():Object{
		  object[nm].apply(object, arguments);
		  return wrapper;
	    }
	   })(nm);
  }
  for each(i in structure.accessor){
	 access = i.@access;
	 nm = i.@name;
	 if (access == "readonly"){
		 (function(nm):void{
		     var o = $(object[nm]);
		     wrapper[nm] = function():*{
		       return o
			 }
		 })(nm);
	 }else if (access == "readwrite"){
		 (function(nm):void{
			 wrapper[nm] = function(value:*):Object{
				 object[nm] = value;
				 return wrapper;
			 }
		 })(nm);
	 }
  }
  wrapper.add = wrapper.addEventListener;
  wrapper.remove = wrapper.removeEventListener;
  wrapper.appendTo = function(target):Object{
	  target.addChild(object);
	  return wrapper;
  }
  wrapper.prependTo = function(target):Object{
	  target.addChildAt(object, 0);
	  return wrapper;
  }
  wrapper.attr = function(props):Object{
	  for (var i:String in props){
		 object[i] = props[i];
	  }
	  return wrapper;
  }
  return wrapper;
}

A few things worth noting. I did a little speed test against with statements which is pretty funny, the zQuery version takes 9ms and the with statements take less than 1ms. It might not be obvious, but the rect and the line vars are set to reference graphics objects not the sprites – there are a bunch of different ways to handle that…. but I’d never use this anywhere for real so I may not bother fixing it up. The attr() function is actually a useful utility to have hanging around – I’ve mentioned variations on it on actionsnippet many times.

A long time ago I saw this somewhere as3Query I’ve messed with it a bit – sort of tries to bring jQuery style syntax to AS. Worth checking out, not sure I’d use it on a real AS project though…



 
 
 

Leave a Reply

Spam protection by WP Captcha-Free