Search code examples
javascriptjquerynamespacesintrospection

javascript namespace as string


I have the following namespace (com.myCompany.myProject):

var com = {
  myCompany: {
    myProject: {
      x: 1,
      y: 2,
      o: function() {
      }
    }
  }
};

For example, I have the following code:

var self = com.myCompany.myProject;

How can I show this namespace as a string, e.g. "com.myCompany.myProject" ?

I've tried JSON.stringify() but it isn't that I looked for.

Thanks,


Solution

  • It's not possible. An object has no way of knowing where it is stored.

    If you need this kind of functionality, you have to store it somewhere in the object.

    var com = {
      myCompany: {
        myProject: {
          x: 1,
          y: 2,
          '_namespace': 'com.myCompany.myProject',
          o: function() {
          }
        }
      }
    };