locked
What is wroing with my OOP Class's instance methods ? What are instance methods and what are static methods? What am I missing here?

    Question

  • I've following function in one javascript file.

    var UserName;
    (function () {
        
        var service = WinJS.Class.define(function () {
            that = this;
            that.db = null;
            that.path = getFullPath();
            that.username = { get: function () { return that.selectUser(); } }
           },
    
        {
            insertUser: function (userid, username) {
                SQLite3JS.openAsync(that.path)
                  .then(function (myDB) {
                      that.db = myDB;
                      console.log("DB opened for insert service");
                      return that.db.runAsync("INSERT INTO UserValues(userid, username) VALUES(:userid, :username)",
                          {userid: userid, username: username})
                  })
                   .done(function () {
                       console.log("new user inserted" + username);
                   },
                   function (error) {
                       console.log("Error inserting user");
                   })
            }
        },
    {
            insertFactVals: function (userid, factLvl, no1, no2, iteration) {
                //codr
        },
    
    {
            selectUser: function () {
                SQLite3JS.openAsync(that.path)
                  .then(function (myDb) {
                      that.db = myDb;
                      console.log("Service opened for select user");
                      return that.db.eachAsync("SELECT * FROM UserValues WHERE userid = 101", function (row) {
                          console.log("USER : " + row.username + "ID : " + row.userid);
                          //that.username = row.username;
                          //userName = row.username;
                          //UserName = row.username;
                          //console.log("USER : " + UserName);
                          return row.username;
                      })
                      .done(function () {
                          console.log("user inserted ");
                      }, function (error) {
                          console.log("Error inserting user");
                      })
                  });
            }
        }
    
        );
     //make function public:
        WinJS.Namespace.define("DBData",
            {
                Service: service,
                //UserName: UserName
            });
    
    })();
        
    

    To call this method, I've following :

    var dbService = new DBData.Service();

    dbService.insertUser(101, "SSS");  //works ok. get's called.

    dbService.selectUser(); //does not work. Gives error, "selectUSer" method is not available.

    I tried it as follows:

    DBData.Service.selectUser(); // It does not work either. Object doesn't support property or method selectUser().

    As far as I understand all these methods are instance methods. And I can call them dbService.method(). But it doesn't work. Why? What am I missing_


    sonal

    Saturday, September 06, 2014 5:12 AM

Answers

  • You have and extra }, such that you're passing four arguments to WinJS.Class.define instead of just three. The {} block } containing insertFactVals is the third argument for static members. This makes your { } block with selectUser the fourth argument, which is ignored in JavaScript without warning and therefore selectUser is not included in the definition.

    Here's the basic structure without the function bodies:

    var service = WinJS.Class.define(function () {
            //Constructor
        },
        {
            //Instance members
            insertUser: function (userid, username) {
            },     
        
            insertFactVals: function (userid, factLvl, no1, no2, iteration) {
            },
        },
        {
            //Static members
            selectUser: function () {
            }
        }
    );

    When you call a static member, you then need to do so through the fully-qualified name, which in your case will be DBData.Service.selectUser(). Given the structure above, if you want to make selectUser an instance member you can move it up into the right set of {}'s. It's really helpful to keep a structure of the nested braces with comments so you can clearly see where to add members.

    Kraig

    Author, Programming Windows Store Apps with HTML, CSS, and JavaScript, Second Edition, a free ebook from Microsoft Press.



    • Marked as answer by SonalMac Sunday, September 07, 2014 6:14 AM
    Saturday, September 06, 2014 2:51 PM

All replies

  • The method selectUser does not exist in the context please generate a new method stub for it and do all the working in that it does not seem that you have made something static but please check that the method selectUser which you are calling exist or not. Moreover the static method is a method who can use our static variable or objects as well as normal but a normal method cannot access static variable or objects however static methods can be called by both static variable or object as well as normal but a static variable or object cannot call a normal method.

    Hope this will help.

    Muhammad Asad.

    Saturday, September 06, 2014 6:39 AM
  • You have and extra }, such that you're passing four arguments to WinJS.Class.define instead of just three. The {} block } containing insertFactVals is the third argument for static members. This makes your { } block with selectUser the fourth argument, which is ignored in JavaScript without warning and therefore selectUser is not included in the definition.

    Here's the basic structure without the function bodies:

    var service = WinJS.Class.define(function () {
            //Constructor
        },
        {
            //Instance members
            insertUser: function (userid, username) {
            },     
        
            insertFactVals: function (userid, factLvl, no1, no2, iteration) {
            },
        },
        {
            //Static members
            selectUser: function () {
            }
        }
    );

    When you call a static member, you then need to do so through the fully-qualified name, which in your case will be DBData.Service.selectUser(). Given the structure above, if you want to make selectUser an instance member you can move it up into the right set of {}'s. It's really helpful to keep a structure of the nested braces with comments so you can clearly see where to add members.

    Kraig

    Author, Programming Windows Store Apps with HTML, CSS, and JavaScript, Second Edition, a free ebook from Microsoft Press.



    • Marked as answer by SonalMac Sunday, September 07, 2014 6:14 AM
    Saturday, September 06, 2014 2:51 PM
  • Thank you very much, Kraig..  It's what I was making my head bumped against so hard... thanks for making it clear...  But I've another question.. pls. lokk at it too...http://social.msdn.microsoft.com/Forums/en-US/5b7df210-a51f-4ba5-a6be-95af3eef3641/how-to-return-a-value-from-a-function-to-other-pages?forum=winappswithhtml5

    sonal


    • Edited by SonalMac Sunday, September 07, 2014 6:19 AM
    Sunday, September 07, 2014 6:14 AM