locked
About read-only properties...

    Question

  • I’m using this code that simply defines prop2 as read-only using both WinJS and standard object literal notation:

    WinJS.Namespace.define("Test.ViewModel", {
                        prop1: 1,
                        get prop2() {
                            return this.prop1 + 1;
                        }
                    });

                    var vm2 = {
                        prop1: 1,
                        get prop2() {
                            return this.prop1 + 1;
                        }
                    };

     

    The problem is that with WinJS declaration, prop2 is called only once at definition time

                    var vm = Test.ViewModel;
                    vm.prop1 = 10;
                    var res = vm.prop2;  //Not invoked, returns 2              


    while is ok with object literal:               

                    vm2.prop1 = 10;
                    var res2 = vm2.prop2;  //11


    what am I missing?


    Corrado Cavalli [Microsoft .NET MVP-MCP]
    UGIdotNET - http://www.ugidotnet.org
    Weblog: http://blogs.ugidotnet.org/corrado/
    Twitter: http://twitter.com/corcav

    Thursday, July 19, 2012 3:08 PM

Answers

  • When using WinJS define your property this way:

    prop2: {
        get: function () {
            return this.prop1 + 1;
        }
    },

    Tim Greenfield

    Thursday, July 19, 2012 3:40 PM

All replies

  • When using WinJS define your property this way:

    prop2: {
        get: function () {
            return this.prop1 + 1;
        }
    },

    Tim Greenfield

    Thursday, July 19, 2012 3:40 PM
  • Thanks Tim! :)

    Corrado Cavalli [Microsoft .NET MVP-MCP]
    UGIdotNET - http://www.ugidotnet.org
    Weblog: http://blogs.ugidotnet.org/corrado/
    Twitter: http://twitter.com/corcav

    Thursday, July 19, 2012 4:30 PM