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

Namespaces

Expand Messages
  • gradescript
    Hello i was wondering if its only possible to extend off the YAHOO namespace or if you can create your own namespace. For example i want to make like
    Message 1 of 2 , Jul 1 7:07 PM
      Hello i was wondering if its only possible to extend off the YAHOO
      namespace or if you can create your own namespace.

      For example i want to make like

      Product.class instead of YAHOO.product.class as what i am creating has
      MANY classes.
    • alexshusta
      Yes. The simplest way to do this would be to work within the existing namespace hierarchy and then use alias to shorten the names down. Building upon the
      Message 2 of 2 , Jul 2 6:53 AM
        Yes.

        The simplest way to do this would be to work within the existing namespace hierarchy and then use alias' to shorten the names down. Building upon the example at http://developer.yahoo.com/yui/yahoo/#namespace   :

        <script type="text/javascript" src="http://yui.yahooapis.com/2.2.2/build/yahoo/yahoo-min.js" ></script>
        <script>
            // Creates a namespace for "myproduct1"
            YAHOO.namespace ("myproduct1");
            YAHOO.myproduct1.Class1 = function(info) {
                alert(info);
            };
           
            // Creates a namespace for "myproduct2", and for "mysubproject1"
            YAHOO.namespace ("myproduct2.mysubproject1");
            YAHOO.myproduct2.mysubproject1.Class1 = function(info) {
                alert(info);
            }
            YAHOO.myproduct1.Class1("boop");
           
            var myP1 = YAHOO.myproduct1;
            myP1.Class1("bip");
           
            var myP2sub = YAHOO.myproduct2.mysubproject1;
            myP2sub.Class1("bop");
        </script>

        Here we're creating two new namespaces, then aliasing them to one word vars. Note that the alias is performed outside of any function giving it a global scope.

        Alternatively, if you look at the yahoo.js source code, it looks pretty easy to create your own top-level namespace by starting with an empty object class and mixing in the same additional functionality as the YAHOO class does. There's a nice, syntax highlighted, version of the file at http://developer.yahoo.com/yui/docs/YAHOO.js.html if you want to take a look.

        Hope that helps,
        ~Alexander


        --- In ydn-javascript@yahoogroups.com, "gradescript" <joshcow@...> wrote:
        >
        > Hello i was wondering if its only possible to extend off the YAHOO
        > namespace or if you can create your own namespace.
        >
        > For example i want to make like
        >
        > Product.class instead of YAHOO.product.class as what i am creating has
        > MANY classes.
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.