Loading ...
Sorry, an error occurred while loading the content.
 

Re: Having trouble seeing how Namespaces and classes work

Expand Messages
  • iskitzdev
    Hi, Sorry I couldn t write back sooner. Here s an explanation by code. If the code with comments doesn t help, let me know. // Guarantee that com.yahoo
    Message 1 of 2 , Jul 3, 2007
      Hi,
      Sorry I couldn't write back sooner. Here's an explanation by code. If
      the code with comments doesn't help, let me know.


      // Guarantee that com.yahoo namespace exists.
      Namespace("com.yahoo");

      // Define your own namespace com.yahoo.lee_1186
      // The "new" keyword executes the code within the curly braces thereby
      defining your variables and classes.
      com.yahoo.lee_1186 = new function()
      {
      // The foos array is now out of the global namespace.
      var foos;

      // Define Foo class
      function Foo(name)
      {
      this.name = name;
      foos[name] = this;
      }

      // Define common who method of Foo class
      Foo.prototype.who = function()
      {
      alert(this.name);
      }

      // Make Foo class available via your custom namespace ( i.e.
      com.yahoo.lee_1186.Foo )
      this.Foo = Foo;

      // Make Foo class available in the global namespace ( i.e. var aFoo =
      new Foo('bob'); )
      window['Foo'] = Foo;
      };

      Good Luck!

      --- In ajile@yahoogroups.com, "lee_1186" <lee_1186@...> wrote:
      >
      > I have some existing classes that I want to wrap up on a namespace and
      > I'm having trouble seeing how that's going to work.
      >
      > Let's say I have:
      >
      > var foos;
      >
      > function Foo(name) {
      > this.name = name;
      > foos[name] = this;
      > }
      >
      > Foo.prototype.who = function() {
      > alert(this.name);
      > }
      >
      > Bar.prototype = new Foo;
      > Bar.prototype.contructor = Bar;
      >
      > function Bar(name,stuff) {
      > Foo.call(this,name);
      > this.stuff = stuff;
      > }
      >
      > ...
      >
      > var aFoo = new Foo('bob');
      > var aBar = new Bar('bill',{a:'x', b:'y'});
      >
      > My goal is to get the foos array out of the global namespace and I
      > assume I will, don't mind if I do, push the classes down there, too so
      > I have:
      >
      > Namespace('something');
      >
      > ...
      >
      > var aBar = new something.Bar('bill',{a:'x', b:'y'});
      >
      > but I don't know what to put in the ... to make that happen. Is there
      > a classes in namespaces example I missed? TIA.
      >
    Your message has been successfully submitted and would be delivered to recipients shortly.