GWT应用程序中使用的Javascript通用clone()方法 [英] Javascript generic clone() method used in GWT application

查看:117
本文介绍了GWT应用程序中使用的Javascript通用clone()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个通用的克隆函数,它应该能够进行真正的深度克隆。我遇到了这个链接,如何深入克隆在JavaScript 并采取功能从那里。



当我尝试使用直接Javascript时,该代码工作得很好。我在代码中做了小的修改,并尝试在GWT中放入JSNI代码。

克隆功能:

  deepCopy = function(item)
{
if(!item){
return item;
} // null,未定义值检查

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

//如果有人做了新的String('aaa')或新的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){//抛出的异常
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;
}
}

返回结果;
}

传递给此函数的列表如下所示:

 列表<整数> list = new ArrayList< Integer>(); 
list.add(new Integer(100));
list.add(new Integer(200));
list.add(new Integer(300));

列表<整数> newList =(List< Integer>)new Attempt()。clone(list);

整数temp =新整数(500);
list.add(temp);

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

但是当我执行此操作时,我立即在克隆函数中得到空指针异常 alert(2nd)行。

请帮忙。



PS:我想在这里得到一个可用于克隆任何对象的通用克隆方法。

解决方案

GWT原型对象没有forEach方法;它们不会继承标准的JavaScript对象原型,因为它们应该像Java对象那样操作,而不是JavaScript对象。



你可能会抛弃Object.prototype.forEach。 call(item,function(){})

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.

解决方案

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(){})

这篇关于GWT应用程序中使用的Javascript通用clone()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆