locked
WinJS.Class.derive doesn't seem to copy static methods from the base class to the derived class

    Question

  • foo = WinJS.Class.define(null,null,{
      loadAsync: function(path)
    });
    
    bar = WinJS.Class.derive(foo,null,null)
    I was expecting "bar" to also contain "loadASync". Is that an oversight?
    Friday, March 28, 2014 4:22 PM

Answers

  • You can actually access the static method of foo using this approach:

                    var foo = WinJS.Class.define(null, null, {
                                                    loadAsync: function (test) {
                                                        console.log("foo loadAsync called: " + test);
                                                    }
                                                });
    
                    var bar = WinJS.Class.derive(foo, null, null, null);
                    foo.loadAsync("from foo");
                    bar.prototype.__proto__.constructor.loadAsync("from bar");


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Friday, March 28, 2014 7:19 PM
    Moderator
  • The definition of the derive() only seems to instantiate the static member for the derived class only in base.js, my thinking is that it was based because static methods/ fields are more for a class level. If you wanted to prevent the above code, and make it syntactically easier to reference the method, you could probably re-declare your method in the derived class :) and do something like this, everything else being the same:

    var bar = WinJS.Class.derive(foo, null, null, { loadAsync: foo.loadAsync});
    bar.loadAsync("from bar");


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog


    Friday, March 28, 2014 8:27 PM
    Moderator

All replies

  • You can actually access the static method of foo using this approach:

                    var foo = WinJS.Class.define(null, null, {
                                                    loadAsync: function (test) {
                                                        console.log("foo loadAsync called: " + test);
                                                    }
                                                });
    
                    var bar = WinJS.Class.derive(foo, null, null, null);
                    foo.loadAsync("from foo");
                    bar.prototype.__proto__.constructor.loadAsync("from bar");


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Friday, March 28, 2014 7:19 PM
    Moderator
  • Thanks. I know that, I just wondered why WinJS.class.derive did not make it easier for us devs.
    Friday, March 28, 2014 8:12 PM
  • The definition of the derive() only seems to instantiate the static member for the derived class only in base.js, my thinking is that it was based because static methods/ fields are more for a class level. If you wanted to prevent the above code, and make it syntactically easier to reference the method, you could probably re-declare your method in the derived class :) and do something like this, everything else being the same:

    var bar = WinJS.Class.derive(foo, null, null, { loadAsync: foo.loadAsync});
    bar.loadAsync("from bar");


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog


    Friday, March 28, 2014 8:27 PM
    Moderator