:: Forum >>

Object.prototype bug?

If I override the prototype object with any other methods, the ActiveWidgets grid component always shows the first row of data as 'undefined'? A bug or am I implementing something incorrectly?
WB - Orlando
Monday, February 23, 2004
Can you please provide an example, which prototype object you modify?
Alex (ActiveWidgets)
Monday, February 23, 2004
I use an object deep cloning function within my applications which is attached to the object.prototype function like below... If you include this anywhere in the page scope of the grid, the first row shows up as an undefined but does not seem to interfere with the mechanics of the grid at all...

Object.prototype.clone = function(){
if(typeof(this) != "object"){
return this;
}
var cloneDepth = ((arguments.length >= 1)?((isNaN(parseInt(arguments[0])))?(null):parseInt(arguments[0])):null);
if (cloneDepth){
cloneDepth=((cloneDepth <= 0)?(null):(cloneDepth));
}
var cloneObject = null;
var thisConstructor = this.constructor;
var thisConstructorPrototype = thisConstructor.prototype;
if (thisConstructor == Array){
cloneObject = new Array();
} else if(thisConstructor == Object){
cloneObject = new Object();
} else {
try{
cloneObject = new thisConstructor;
} catch(exception) {
cloneObject = new Object();
cloneObject.constructor = thisConstructor;
cloneObject.prototype = thisConstructorPrototype;
}
}
var propertyName = "";
var newObject=null;
for (propertyName in this){
newObject = this[propertyName];
if (!thisConstructorPrototype[propertyName]){
if (typeof(newObject)=="object"){
if (newObject === null){
cloneObject[propertyName] = null;
} else {
if(cloneDepth){
if(cloneDepth == 1){
cloneObject[propertyName] = null;
} else {
cloneObject[propertyName] = newObject.clone(--cloneDepth);
}
} else {
cloneObject[propertyName] = newObject.clone();
}
}
} else {
cloneObject[propertyName] = newObject;
}
}
}
return cloneObject;
};
WB - Orlando
Tuesday, February 24, 2004
Sorry, I cannot replicate this - if I just add this code to any of the examples (/examples/grid/...) everything is OK. So I suspect it might be something else. ???
Alex (ActiveWidgets)
Tuesday, February 24, 2004
It is not a good idea top add properties to Object's prototype. When you do, the new property is going to become an enumerable property of all objects (since everything inherits from object).

The consequence is that when you do a for in loop, the clone method that you added to the prototype will always be one of the enumerated properties.

I am pretty sure this is what is causing the error, even though I didn't even look at ActiveWidgets' code.

Workaround? Instead of extending Object (which I wish I could do), just create helper methods. Mine is called JSUtil.cloneObject( objToClone ).
Juan Mendes (<PUT_FIRST_NAME_HERE>itophp@yahoo.com
Thursday, April 6, 2006

This topic is archived.


Back to support forum

Forum search