Javascript data structures

class-designjavascript

I would like to know if it is possible to do something like this:

if (mb == null || typeof (mb) != "object") {
    var mb = new Object();
}


mb = {
    tests: {
        onAnimals: {
            test: function() {
                return "";
            }
        }
        onHumans: {
            test: function() {
                return "";
            }
        }
    }
}

Bu when I tries it, I can see Tests, but when I dot further in, I can't se onAnimals / onHumans.

javascript is still new to me, so hope you can help.

Best Solution

You're missing a comma before onHumans. I've assumed mb to be a global variable; you can use var instead if that's what you need. Also, it's easier to read if you structure it differently, like this:

window.mb = window.mb || {};
window.mb = {
  tests: {
    onAnimals: {
      test: function(){
        return "";
      }
    },
    onHumans: {
      test: function(){
        return "";
      }
    }
  }
};