JSB 下的深拷贝

来源:互联网 发布:php怎么读 编辑:程序博客网 时间:2024/06/05 05:32
在JSB环境下,h5模式的cc.clone是使用不了。

解决方案:

将 cc.clone 拷贝到 jsb_prepare.js


cc.clone = function (obj) {    // Cloning is better if the new object is having the same prototype chain    // as the copied obj (or otherwise, the cloned object is certainly going to    // have a different hidden class). Play with C1/C2 of the    // PerformanceVirtualMachineTests suite to see how this makes an impact    // under extreme conditions.    //    // Object.create(Object.getPrototypeOf(obj)) doesn't work well because the    // prototype lacks a link to the constructor (Carakan, V8) so the new    // object wouldn't have the hidden class that's associated with the    // constructor (also, for whatever reasons, utilizing    // Object.create(Object.getPrototypeOf(obj)) + Object.defineProperty is even    // slower than the original in V8). Therefore, we call the constructor, but    // there is a big caveat - it is possible that the this.init() in the    // constructor would throw with no argument. It is also possible that a    // derived class forgets to set "constructor" on the prototype. We ignore    // these possibities for and the ultimate solution is a standardized    // Object.clone(<object>).    var newObj = (obj.constructor) ? new obj.constructor : {};    // Assuming that the constuctor above initialized all properies on obj, the    // following keyed assignments won't turn newObj into dictionary mode    // because they're not *appending new properties* but *assigning existing    // ones* (note that appending indexed properties is another story). See    // CCClass.js for a link to the devils when the assumption fails.    for (var key in obj) {        var copy = obj[key];        // Beware that typeof null == "object" !        if (((typeof copy) === "object") && copy &&            !(copy instanceof cc.Node)) {            newObj[key] = cc.clone(copy);        } else {            newObj[key] = copy;        }    }    return newObj;};


0 0
原创粉丝点击