سؤال

I have this function for create objects in 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;
        }
}

Basicly creates BiometricSignatureData-type objects with 2 methods

  • restartObject
  • printBiometricValuesOnRealTime

and then I created two instances of the same object

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

my first instance (biometricObject) is filled with data, then I want to clone (not by reference) this instance in my second instance (biometricObjectBackup) by doing this:

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;
            }

filling second instance with first instance info

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);

put three console.log to follow the behavior of the objects getting next output:

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

If I restart my original object, the second one (the cloned) cleans itself as well, how to do this right???

thanks for the support

هل كانت مفيدة؟

المحلول

you may try this:

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;

clone function is the same

and the last part:

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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top