P
ROTOTYPE
I'm Not Only The Prototype, I'm Also A Member.
home
▪
stats
▪
search
▪
linkback
▪
about
▪
FAQ
| user: guest,
login
,
register
Functions
:
the infamous for...in BUG overrided
author:
zwetan
[+]
,
Submitted: 10.26.01 4a
/* author: zwetan Kjukov date: 2001-10-26 the for ... in is BUGGED it should NOT prevent you to do real OOP */ // global function to replace typeof() // in order to be able to distinguish // different type of objects Object.prototype.TypeOfClass = function(arg) { if(arg._class == null) {return(typeof(arg));} else {return(arg._class.toLowerCase());} } // method for all objects ro return the class and version info of an Object Object.prototype.Info = function() {return(this._class+" "+this._version);} // Array.prototype._class = "Array"; // Array.prototype._version = "1.1"; // test = new Array("a","b","c"); // test.info(); // trace(typeOfClass(test); // Depolute an object before doing a for...in Object.prototype.Depolute = function(target,isClass) { if(typeOfClass(target) == "movieclip") {return;} if(isClass == true) {target.__proto__.__proto__ = target.prototype;} else {target.__proto__ = target.prototype;} } // Repolute an object after a for...in to keep the __proto__ chain unbreaked Object.prototype.Repolute = function(target,isClass) { if(typeOfClass(target) == "movieclip") {return;} if(isClass == true) {target.__proto__.__proto__ = Object.prototype;} else {target.__proto__ = Object.prototype;} } // enumerate any object Object.prototype.Enum = function(target) { var prop, data, i, j, me, isClass; if(target == null) {target = this; isClass = true;} me = TypeOfClass(target); data = []; Depolute(target,isClass); for(prop in target) { //trace(prop+" - "+target[prop]); data.push({name:prop, value:target[prop], type:typeof(target[prop])}); } Repolute(target,isClass); trace("-----------"); trace("enum: "+me); for(i=data.length-1,j=0; i>-1;i--,j++) { trace(j+" name:"+data[i].name+" value:"+data[i].value+" type:"+data[i].type); } trace("-----------"); } //usage // Enum(); //enumerate the curente timeline // test = new Array("a","b","c"); // test.Enum(); //enumerate as a method of the instance // Enum(test); // enumerate the instance itselft // global function to test if an arg passed is a Class constructor Object.prototype.IsClass = function(target) { var test, data; test = new this[target](); ((TypeOfClass(test) != "undefined")&&(test._class != null))? data = true: data = false; return data; } // global function to test if an arg passed is a Function Object.prototype.IsFunction = function(target) { var data; if(IsClass(target) == true) {return false;} TypeOfClass(this[target]) == "function" ? data = true: data = false; return data; }
usage
// some test now Array.prototype._class = "Array"; Array.prototype._version = "1.0"; // a custom defined object which act as a native predefined object Object.prototype.Custom = function() { this._class = "Custom"; this._version = "1.0"; this.data = []; if(arguments.length > 0) { for(var i=0;i<arguments.length;i++) { this.insert(arguments[i]); } } } Custom.prototype.insert = function(data) { this.data.push(data); } //predefined object instancied toto = {FR:"bonjour", USA:"good morning", CN:"ni hao", JP:"konichiwa"}; Enum(toto); //custom object instancied titi = new Custom("hello world","coucou","blah bla"); titi.Enum(); Enum(); //another predefined object instancied // notice that the type returned is Array not Object :)) test = new Array("a","b","c"); // notice the different values returned depending you // treat the Enum as a global function or a nethod to the object // is what I call inheritance :)) test.Enum(); Enum(test);
Add Comment
[+]
›opyleft 2001-2010. Layer51 is: Jaime Prado.
@