Code.Flash MX: Correction to Object.toAS() version 2!
Ampre's code has a bug in handling Array of strings (refer to my previous post). Since he is just enclosing the result of original Array.toString() with square backets it returns [a,b,10] when it is supposed to return ['a', 'b' ,10]. Just replacing his version of Array.prototype.toString with the following code fixes it.
Array.prototype.toString = function() {
var s;
for (var i = 0; i<this.length; i++) {
s += ((typeof (this[i]) == 'string') ? "'"+this[i]+"'" : this[i])+",";
}
if (s.length) {
s = substring(s, 1, s.length-1);
}
return "["+s+"]";
};
posted by Arul
| link
|<prev. | ^top
| next> |
add comment |
Code.Flash MX: Better Object-toAS() Code.
As you all already know my Object.toAS() code does fail with circular references. Ampre has replied with the following code in prototype, which is sleeker and also can addresses the circular reference issue to some extent.
Here is that code
Object.prototype.toString = function() {
var s;
if (this.$tmpsvar) {
return "this";
}
this.$tmpsvar = true;
ASSetPropFlags(this, "$tmpsvar", 1);
for (var i in this) {
s += i+":"+((typeof (this[i]) == 'string') ? "'"+this[i]+"'" : this[i])+",";
}
if (s.length) {
s = substring(s, 1, s.length-1);
}
delete this.$tmpsvar;
return "{"+s+"}";
};
Array.prototype.$toString = Array.prototype.toString;
Array.prototype.toString = function() {
return "["+this.$toString()+"]";
};
ASSetPropFlags(Array.prototype, ["$toString"], 1);
Usage Example
qz = {abcd:20, ddsasd:88, phft:"cat", dd:{aaa:33, no:false, yes:true}};
qz.qzzz = qz;
trace(qz);
//traces '{qzzz:this,abcd:20,ddsasd:88,phft:'cat',dd:{aaa:33,no:false,yes:true}}'
posted by Arul
| link
|<prev. | ^top
| next> |
add comment |