浏览此链接时 How to Deep clone in javascript
我遇到了一个通用的克隆方法(在接受的答案中)。我尝试直接在 javascript 中运行它,它运行时给出了完美的输出。
我将该代码放在 native 标记中,并尝试在 GWT 中克隆一个对象。
我要克隆的类是
private class Container
{
Integer i = 5;
}
当我尝试这样做时,它只是返回相同的对象。有人可以帮忙吗? 有什么不清楚的可以问我。非常感谢。
最佳答案
Jonathan 是对的:方法(也是 GWT 中唯一的方法)是使用 https://code.google.com/p/google-web-toolkit/wiki/AutoBean
这可能有点尴尬,但它工作得很好,因为许多概念都与之相关(RequestFactory 中的 EntityProxy 也依赖于该机制,它是 GWT 的 future )。
深度 json 持久性也适用于自动 bean。
您唯一需要做的就是创建一个描述您的类的接口(interface)(并在您的类中实现它):
public interface ContainerBean {
Integer getI();
void setI(Integer i);
}
然后创建你的工厂接口(interface)
interface MyFactory extends AutoBeanFactory {
// Factory method for a simple AutoBean
AutoBean<ContainerBean> container();
// Factory method for a non-simple type or to wrap an existing instance
AutoBean<ContainerBean> container(ContainerBean toWrap);
}
现在你可以包装你的对象并克隆它(通过 json since)
clone() An AutoBean and the property values stored within it can be cloned. The clone() method has a boolean parameter that will trigger a deep or a shallow copy. Any tag values associated with the AutoBean will not be cloned. AutoBeans that wrap a delegate object cannot be cloned.
https://code.google.com/p/google-web-toolkit/wiki/AutoBean#clone()
因此改用这个方法: https://code.google.com/p/google-web-toolkit/wiki/AutoBean#AutoBeanCodex
关于javascript - GWT 中的深度克隆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14258486/