Javascript generic clone() method used in GWT appl

2020-07-16 02:51发布

问题:

I was trying to write a generic clone function which should be able to do true deep cloning. I have come across this link, How to Deep clone in javascript and took the function from there.

That code workds pretty well when I try using direct Javascript. I did minor modifications in the code and tried to put in the JSNI code in GWT.

clone function:

deepCopy = function(item)
{
    if (!item) {
        return item;
    } // null, undefined values check

    var types = [ Number, String, Boolean ], result;

    // normalizing primitives if someone did new String('aaa'), or new Number('444');
    types.forEach(function(type) {
        if (item instanceof type) {
            result = type(item);
        }
    });

    if (typeof result == "undefined") {
        alert(Object.prototype.toString.call(item));
        alert(item);
        alert(typeof item);
        if (Object.prototype.toString.call(item) === "[object GWTJavaObject]") {
            alert('1st');
            result = [];
            alert('2nd');
            item.forEach(function(child, index, array) {//exception thrown here
                alert('inside for each');
                result[index] = deepCopy(child);
            });
        } else if (typeof item == "GWTJavaObject") {
            alert('3rd');

            if (item.nodeType && typeof item.cloneNode == "function") {
                var result = item.cloneNode(true);
            } else if (!item.prototype) { 
                result = {};
                for ( var i in item) {
                    result[i] = deepCopy(item[i]);
                }
            } else {
                if (false && item.constructor) {
                    result = new item.constructor();
                } else {
                    result = item;
                }
            }
        } else {
            alert('4th');
            result = item;
        }
    }

    return result;
}

And the list am passing to this function is like this:

List<Integer> list = new ArrayList<Integer>();
        list.add( new Integer( 100 ) );
        list.add( new Integer( 200 ) );
        list.add( new Integer( 300 ) );

        List<Integer> newList = ( List<Integer> ) new Attempt().clone( list );

        Integer temp = new Integer( 500 );
        list.add( temp );

        if ( newList.contains( temp ) )
            Window.alert( "fail" );
        else
            Window.alert( "success" );

But when I execute this, I get null pointer exception in the clone function immediately after alert("2nd") line.

Kindly help.

P.S: I am trying to get a generic clone method here that can be used to clone any object.

回答1:

GWT prototyped objects don't have a forEach method; they do not inherit standard javascript object prototypes, as they are supposed to act like java objects, not javascript objects.

You could probably get away with Object.prototype.forEach.call(item, function(){})