locked
array property of WinJS class is not refreshed by constructor RRS feed

  • Question

  • Is the following a bug, or would someone please explain to me why it behaves like this:

    var class0 = WinJS.Class.define(
        function () {
        },
        {
            _value: [0,0],
            value: { get: function () { return this._value; } },
            add: function () {
                this._value[0]++;
            }
        }
    );
    var x = new class0();
    x.add();
    Debug.writeln(x.value); // outputs "1,0"
    x = new class0();
    Debug.writeln(x.value); //why is this still "1,0"? isn't it supposed to be "0,0"?

    Monday, September 3, 2012 5:33 PM

Answers

  • This is by design as the members defined as part of the WinJS.Class.define call are all "prototype" members of the instances which are per-type and not per-instance. This does mean that any members which have mutable structure (arrays, objects) need to be initialized in the constructor explicitly.

    -josh

    Monday, September 3, 2012 8:38 PM

All replies

  • This is by design as the members defined as part of the WinJS.Class.define call are all "prototype" members of the instances which are per-type and not per-instance. This does mean that any members which have mutable structure (arrays, objects) need to be initialized in the constructor explicitly.

    -josh

    Monday, September 3, 2012 8:38 PM
  • wow that is crazy.  this throws anyone who has done OO design/dev for a loop! 

    also, is this documented somewhere?  i couldn't find it.

    Thursday, May 16, 2013 6:58 PM