Object.prototype.clone

Garrett Smith dhtmlkitchen at gmail.com
Mon Oct 22 02:09:50 PDT 2007


cloneObject( this ); // Error if this is window.

// enumeration is partially broken in IE, this loop will sometimes fail
for( var property in srcObj )



On 10/22/07, Eugen.Konkov at aldec.com <Eugen.Konkov at aldec.com> wrote:
> I have clone objects like this:
> //----------------------------------------------------
> function cloneObject( srcObj ) {
>   if( srcObj == null ) { return srcObj; } //undefined or null
>
>   var newObject;
>   switch( typeof(srcObj) ) {
>     case "object":
>       newObject = new srcObj.constructor();
>       for( var property in srcObj ) {
>        //Do not clone inherited values
>        if( srcObj.hasOwnProperty(property) || typeof( srcObj[property] ) ===
> 'object' ) {
>          newObject[property]= cloneObject( srcObj[property] );
>          }
>        }
>       break;
>
>     default:
>       newObject = srcObj;
>       break;
>     }
>
>   return newObject;
>   }
>
>
> ----- Original Message -----
> From: "Peter Michaux" <petermichaux at gmail.com>
> To: <es4-discuss at mozilla.org>
> Sent: Monday, October 22, 2007 5:07 AM
> Subject: Object.prototype.clone
>
>
> > Hi,
> >
> > Is there a plan for a "clone" function in ES4?
> >
> > Object.prototype.clone = function() {
> >        function F() {}
> >        F.prototype = o;
> >        return new F();
> > };
> >
> > The earliest reference I have found to this function is a post by
> > Lasse Reichstein Nielsen in 2003 on comp.lang.javascript
> >
> > http://groups.google.com/group/comp.lang.javascript/msg/5d06e72e55d5bf11
> >
> > In the past year this function has been evangelized by Douglas Crockford.
> >
> > http://javascript.crockford.com/prototypal.html
> >
> > Given the idea has persisted for at least a four year period as
> > something useful in a prototype-based language, would a clone function
> > be a good addition to the language itself?
> >
> > Peter
> > _______________________________________________
> > Es4-discuss mailing list
> > Es4-discuss at mozilla.org
> > https://mail.mozilla.org/listinfo/es4-discuss
>
> _______________________________________________
> Es4-discuss mailing list
> Es4-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es4-discuss
>


-- 
Programming is a collaborative art.



More information about the Es4-discuss mailing list