如何正确克隆WinJS中的对象

时间:2014-02-13 00:07:59

标签: object visual-studio-2013 clone winjs

我在WinJS中创建了这个对象的函数:

function BiometricSignatureData() {
    this.averageSpeed       = "";
    this.pressure           = new Array();
    this.location           = new Array();
    this.timestamp          = new Array();
    this.speed              = new Array();
    this.evt                = new Array();
    this.spaces             = 0;
    this.attackPoints       = 0;
    this.finalPoints        = 0;
    this.signatureAddress   = "";


    this.restartObject =
        function () {
            this.pressure           = new Array();
            this.location           = new Array();
            this.timestamp          = new Array();
            this.averageSpeed       = "";
            this.speed              = new Array();
            this.evt                = new Array();
            this.spaces             = 0;
            this.attackPoints       = 0;
            this.finalPoints        = 0;
            this.signatureAddress   = "";
        }
    this.printBiometricValuesOnRealTime = 
        function (pressure, location, averageSpeed, spaces, attackPoints, finishPoints) {

            pressure.value                = this.pressure[this.pressure.length -1];
            location.value                = "{" + this.location[this.location.length - 1] + "}";
            averageSpeed.value            = this.averageSpeed;
            spaces.value                  = this.spaces;
            attackPoints.value            = this.attackPoints;
            finishPoints.value            = this.finalPoints;
        }
}

使用2种方法

基本创建BiometricSignatureData类型对象
  • restartObject
  • printBiometricValuesOnRealTime

然后我创建了同一个对象的两个实例

var biometricObject         = new BiometricSignatureData();
var biometricObjectBackup   = new BiometricSignatureData();

我的第一个实例(biometricObject)充满了数据,然后我想在我的第二个实例(biometricObjectBackup)中克隆(而不是通过引用)这个实例:

function cloneObject(from, to) {// extends 'from' object with members from 'to'. If 'to' is null, a deep clone of 'from' is returned
                if (from == null || typeof from != "object") return from;
                if (from.constructor != Object && from.constructor != Array) return from;
                if (from.constructor == Date || from.constructor == RegExp || from.constructor == Function ||
                    from.constructor == String || from.constructor == Number || from.constructor == Boolean)
                    return new from.constructor(from);
                to = to || new from.constructor();
                for (var name in from) {
                    to[name] = typeof to[name] == "undefined" ? cloneObject(from[name], null) : to[name];
                }
                return to;
            }

使用第一个实例信息填充第二个实例

console.log("original Array: " + biometricObject.evt.length + ",  backup Array: " + biometricObjectBackup.evt.length);
biometricObjectBackup = cloneObject(biometricObject);

console.log("original Array: " + biometricObject.evt.length + ",  backup Array: " + biometricObjectBackup.evt.length);
biometricObject.restartObject();
console.log("original Array: " + biometricObject.evt.length + ",  backup Array: " + biometricObjectBackup.evt.length);

放三个console.log来跟踪对象获得下一个输出的行为:

original Array: 10,  backup Array: 0
original Array: 10,  backup Array: 10
original Array: 0,  backup Array: 0

如果我重新启动原始对象,第二个(克隆)也会自行清理,如何正确执行此操作???

感谢您的支持

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

var BiometricSignatureData = {
    averageSpeed:       "",
    pressure:           new Array(),
    location:           new Array(),
    timestamp:          new Array(),
    speed:              new Array(),
    evt:                new Array(),
    spaces:             0,
    attackPoints:       0,
    finalPoints:        0,
    signatureAddress:   "",


    restartObject :
        function () {
            this.pressure           = new Array();
            this.location           = new Array();
            this.timestamp          = new Array();
            this.averageSpeed       = "";
            this.speed              = new Array();
            this.evt                = new Array();
            this.spaces             = 0;
            this.attackPoints       = 0;
            this.finalPoints        = 0;
            this.signatureAddress   = "";
        },

    printBiometricValuesOnRealTime:
        function (pressure, location, averageSpeed, spaces, attackPoints, finishPoints) {

            pressure.value          = this.pressure[this.pressure.length - 1];
            location.value          = "{" + this.location[this.location.length - 1] + "}";
            averageSpeed.value      = this.averageSpeed;
            spaces.value            = this.spaces;
            attackPoints.value      = this.attackPoints;
            finishPoints.value      = this.finalPoints;
        }
}


var BiometricSignatureDataBackup;

克隆功能是相同的

和最后一部分:

BiometricSignatureDataBackup = cloneObject(BiometricSignatureData);

    console.log("original Array: " + biometricObject.evt.length + ",  backup Array: " + biometricObjectBackup.evt.length);
    BiometricSignatureData.restartObject();
    console.log("original Array: " + BiometricSignatureData.evt.length + ",  backup Array: " + BiometricSignatureDataBackup.evt.length);