Shallow Copy of an Object in Javascript

function shallowCopy(orginal) {
    // First create an empty object with
    // same prototype of our original source
    var clone = Object.create(Object.getPrototypeOf(orginal)) ;

    var i, keys = Object.getOwnPropertyNames(orginal) ;
    for (i = 0 ; i < keys.length ; i += 1)
    {
        // copy each property into the clone
        Object.defineProperty(clone, keys[ i ],
            Object.getOwnPropertyDescriptor(orginal, keys[ i ])
        ) ;
    }
    return clone ;
}

Add your comment