0

i have the following script, in which i am trying to create a config object for an application :

  var myConfig = {
  localDBAllowed: function () {
      return window.openDatabase;
  },
  MessageBox: function (message, title) {
      if (navigator.notification) {
          navigator.notification.alert(message, null, title, 'OK');
      } else {
          alert(message);
      }
  },
  initializeDb: function(){
      if (localDBAllowed) {
          messageBox('local db allowed', '');
      } else {
          messageBox('local db not allowed', '');
      }
  }
  };
  myConfig .initializeDb();

the error i get is that localDBAllowed not defined.. from the line : if (localDBAllowed) {

my question is how to access an object's member / method from within the sane object. i tried using this keyword with no success as below:

 if (this.localDBAllowed) {
            messageBox('local db allowed', '');
2
  • When I make the change you tried (changing the condition to this.localDBAllowed) the code works and the condition is executed. Are you perhaps now getting a different error? Commented Jul 30, 2014 at 15:57
  • 1
    Also note that Javascript is case-sensitive, so your calls to messageBox(...) should be this.MessageBox(...) Commented Jul 30, 2014 at 15:59

2 Answers 2

1
this.localDBAllowed

will not return any values, try

this.localDBAllowed()

should do the trick

Sign up to request clarification or add additional context in comments.

1 Comment

you need to execute the function, or this.localDBAllowed will always be true, because you're just testing a function.
0

This worked for me. (Pun intended)

var myConfig = {
  localDBAllowed: function () {
      return window.openDatabase;
  },
  messageBox: function (message, title) {
      if (navigator.notification) {
          navigator.notification.alert(message, null, title, 'OK');
      } else {
          alert(message);
      }
  },
  initializeDb: function (){
      if (this.localDBAllowed) {
          this.messageBox('local db allowed', '');
      } else {
          this.messageBox('local db not allowed', '');
      }
  }
};

myConfig .initializeDb();

All I did was add the this identifier to the areas where functions or variables were called within the object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.