Javascript Namespace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    Javascript Namespace

    Hello,

    How to create a namespace in Javascript containing two methods?
    And how to access those methods?

    Thanks,
    Miguel
  • RobG

    #2
    Re: Javascript Namespace

    On Jun 19, 11:04 am, shapper <mdmo...@gmail. comwrote:
    Hello,
    >
    How to create a namespace in Javascript
    I presume you don't really mean XML namespace. :-)

    The term "namespace" in javascript is sometimes (probably erroneously)
    used to describe a design pattern where functions are added as
    properties of a single object rather than as global functions.

    containing two methods?
    Sounds a lot like homework to me. Create an object, add two
    properties, assign them references to functions.

    And how to access those methods?
    The same way you access any object properties, use either dot or
    square bracket notation as appropriate.

    Have a go, post the result and (almost certainly) you'll get
    constructive criticism.


    --
    Rob

    Comment

    • RoLo

      #3
      Re: Javascript Namespace

      On Jun 18, 9:04*pm, shapper <mdmo...@gmail. comwrote:
      Hello,
      >
      How to create a namespace in Javascript containing two methods?
      And how to access those methods?
      >
      Thanks,
      Miguel
      you mean this:

      Code:
      var name={
      method1:function(){alert(1);},
      method2:function(){alert(2);}
      };
      
      name.method1();
      name.method2();

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: Javascript Namespace

        RobG wrote:
        On Jun 19, 11:04 am, shapper <mdmo...@gmail. comwrote:
        >How to create a namespace in Javascript
        >
        I presume you don't really mean XML namespace. :-)
        >
        The term "namespace" in javascript is sometimes (probably erroneously)
        used to describe a design pattern where functions are added as
        properties of a single object rather than as global functions.
        So-called "global functions" are in fact methods of the Global Object, a
        single object at that. Your statement is only beginning to make sense if
        you say "single *user-defined* object" instead.


        PointedEars
        --
        Prototype.js was written by people who don't know javascript for people
        who don't know javascript. People who don't know javascript are not
        the best source of advice on designing systems that use javascript.
        -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>

        Comment

        • Peter Michaux

          #5
          Re: Javascript Namespace

          On Jun 18, 6:04 pm, shapper <mdmo...@gmail. comwrote:
          Hello,
          >
          How to create a namespace in Javascript containing two methods?
          And how to access those methods?
          RoLo's answer may have been what you were seeking.

          I recently blabbed on and on about this on my blog



          Many of the things I wrote about have been discussed on this group.

          Peter

          Comment

          • Evertjan.

            #6
            Re: Javascript Namespace

            Thomas 'PointedEars' Lahn wrote on 22 jun 2008 in comp.lang.javas cript:
            So-called "global functions" are in fact methods of the Global Object, a
            single object at that. Your statement is only beginning to make sense if
            you say "single *user-defined* object" instead.
            It would be beter to say "default object", or better "root default object",
            rather than "global object", since this default is not a default
            everywhere, but only where the local scope has no other default defined,
            as it must be in the root.

            [Global, in my view means everywhere, so also in local places.]

            It is no matter whether a variable or an object is ment, as it is only the
            named pointer that is affected. Only I do not think it is possible to
            define a local scope for the default object?

            Code:
            <script type='text/javascript'>
            
            var obj = {};
            
            window.a = function(){alert('default-a')};
            obj.a = function() {alert('obj-a')};
            
            a();
            window.a();
            obj.a();
            
            </script>
            --
            Evertjan.
            The Netherlands.
            (Please change the x'es to dots in my emailaddress)

            Comment

            • Richard Cornford

              #7
              Re: Javascript Namespace

              Evertjan wrote:
              Thomas 'PointedEars' Lahn wrote on 22 jun 2008 in
              comp.lang.javas cript:
              >
              >So-called "global functions" are in fact methods of the
              >Global Object, a single object at that. Your statement
              >is only beginning to make sense if you say "single
              >*user-defined* object" instead.
              >
              It would be beter to say "default object", or better "root
              default object", rather than "global object", since this
              default is not a default everywhere, but only where the
              local scope has no other default defined, as it must be in
              the root.
              I don't see that as better. It is getting pretty VKesque; I cannot tell
              what you are talking about by reading the words.
              [Global, in my view means everywhere, so also in local places.]
              The global object is at the end of every scope chain, and so it is
              everywhere (at least in some sense).
              It is no matter whether a variable or an object is ment, as it
              is only the named pointer that is affected. Only I do not think
              it is possible to define a local scope for the default object?
              <snip>

              What do you mean by "default object " here? Is it the object used as the
              'base' of the resulting Reference type when Identifier resolution fails
              to find a corresponding property name on any object on the scope chain?
              That would be the object labelled as "the global object" by the
              language's specification.

              Richard.

              Comment

              • RobG

                #8
                Re: Javascript Namespace

                On Jun 23, 3:34 am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
                wrote:
                RobG wrote:
                On Jun 19, 11:04 am, shapper <mdmo...@gmail. comwrote:
                How to create a namespace in Javascript
                >
                I presume you don't really mean XML namespace. :-)
                >
                The term "namespace" in javascript is sometimes (probably erroneously)
                used to describe a design pattern where functions are added as
                properties of a single object rather than as global functions.
                >
                So-called "global functions" are in fact methods of the Global Object, a
                single object at that. Your statement is only beginning to make sense if
                you say "single *user-defined* object" instead.
                The phrase "rather than" infers some other object than the global
                object. I'm sorry if it is ambiguous.

                The name space object could be any object other than the global
                object, be it native or host. It need not be a user-defined native
                object, though I would expect it to be.


                --
                Rob

                Comment

                • Evertjan.

                  #9
                  Re: Javascript Namespace

                  Richard Cornford wrote on 23 jun 2008 in comp.lang.javas cript:
                  Evertjan wrote:
                  >Thomas 'PointedEars' Lahn wrote on 22 jun 2008 in
                  >comp.lang.java script:
                  >>
                  >>So-called "global functions" are in fact methods of the
                  >>Global Object, a single object at that. Your statement
                  >>is only beginning to make sense if you say "single
                  >>*user-defined* object" instead.
                  >>
                  >It would be beter to say "default object", or better "root
                  >default object", rather than "global object", since this
                  >default is not a default everywhere, but only where the
                  >local scope has no other default defined, as it must be in
                  >the root.
                  >
                  I don't see that as better. It is getting pretty VKesque; I cannot
                  tell what you are talking about by reading the words.
                  I'm sorry.
                  >[Global, in my view means everywhere, so also in local places.]
                  >
                  The global object is at the end of every scope chain, and so it is
                  everywhere (at least in some sense).
                  Now you are just repeating the definition I just pointed to as wrong.
                  >It is no matter whether a variable or an object is ment, as it
                  >is only the named pointer that is affected. Only I do not think
                  >it is possible to define a local scope for the default object?
                  <snip>
                  >
                  What do you mean by "default object " here? Is it the object used as
                  the 'base' of the resulting Reference type when Identifier resolution
                  fails to find a corresponding property name on any object on the scope
                  chain?
                  Now I cannot tell what you are talking about by reading the words.
                  I think you are saying the same as I did in a complicated way.

                  The default object is just the object that is "assumed" if no definite
                  object is specified.

                  So:

                  window.myThing

                  points to the same variable/function/whatever as:

                  myThing

                  unless the local default object is specified otherwize.

                  So methods and values can have a default objects, while objects, methods
                  and values can have a default action/method/etc.

                  I do not think that that is completely implemented in JS, but that is a
                  nice goal for object oriented "namespace naming".
                  That would be the object labelled as "the global object" by the
                  language's specification.
                  Sure, but I object to the word "global" in such specification, as
                  "global" means everywhere, and not "everywhere unless"

                  In the above it just means "default" or "root", if the name space system
                  is seen as a tree. If it is internally a tree does [to me] not matter to
                  the naming.

                  --
                  Evertjan.
                  The Netherlands.
                  (Please change the x'es to dots in my emailaddress)

                  Comment

                  • Lasse Reichstein Nielsen

                    #10
                    Re: Javascript Namespace

                    "Evertjan." <exjxw.hannivoo rt@interxnl.net writes:
                    Thomas 'PointedEars' Lahn wrote on 22 jun 2008 in comp.lang.javas cript:
                    >
                    >So-called "global functions" are in fact methods of the Global Object, a
                    >single object at that. Your statement is only beginning to make sense if
                    >you say "single *user-defined* object" instead.
                    >
                    It would be beter to say "default object", or better "root default object",
                    rather than "global object", since this default is not a default
                    everywhere,
                    Default what?
                    but only where the local scope has no other default defined,
                    as it must be in the root.
                    As it must be *what* in the root?
                    [Global, in my view means everywhere, so also in local places.]
                    The global object is an object, no matter where you are. There must
                    be some property of this object that you think is not globally applicable.
                    It is no matter whether a variable or an object is ment, as it is only the
                    named pointer that is affected. Only I do not think it is possible to
                    define a local scope for the default object?
                    I just don't understand this. Try to explain it with more words.
                    (E.g., do you mean by "default object" what was previously in the thread
                    called "global object"?)

                    /L
                    --
                    Lasse Reichstein Nielsen
                    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                    'Faith without judgement merely degrades the spirit divine.'

                    Comment

                    • Evertjan.

                      #11
                      Re: Javascript Namespace

                      Lasse Reichstein Nielsen wrote on 23 jun 2008 in comp.lang.javas cript:
                      "Evertjan." <exjxw.hannivoo rt@interxnl.net writes:
                      >
                      >Thomas 'PointedEars' Lahn wrote on 22 jun 2008 in
                      >comp.lang.java script:
                      >>
                      >>So-called "global functions" are in fact methods of the Global
                      >>Object, a single object at that. Your statement is only beginning
                      >>to make sense if you say "single *user-defined* object" instead.
                      >>
                      >It would be beter to say "default object", or better "root default
                      >object", rather than "global object", since this default is not a
                      >default everywhere,
                      >
                      Default what?
                      As I wrote in the parallel? posting:

                      The default object is just the object that is "assumed" if no definite
                      object is specified.

                      .... when calling that object in the script.

                      So, in browser js:

                      window.myValue is the same as myValue,
                      unless in a subscope myValue is redefined
                      [= has a pointer to another object/property/method]

                      It would be nice of each subscope could be given a named default subroot
                      object, because now only the myValue of the root scope's and the present
                      scope's myValue are callable, not the in-between-scope's myValue-s.
                      >but only where the local scope has no other default defined,
                      >as it must be in the root.
                      >
                      As it must be *what* in the root?
                      Object. [the default word in this sentence ;-)]
                      The same however is true for all defaulting.

                      >[Global, in my view means everywhere, so also in local places.]
                      >
                      The global object is an object, no matter where you are.
                      Yes, just like any other object, but that's why naming that "global" is not
                      right. It is the root object of the root scope.

                      Theoretically you could set another object as the default object in the
                      root, so that the original root object is no longer the default.
                      [Perhaps not possible in present day JS, but that is only implementation.]

                      Seen as a tree, you could call the object the "global root object",
                      but why shorten that to "global object"? Beter shorten that to "the root
                      object"

                      Even the naming pointer to this object can be overwritten to mean another
                      object, either in the root scope or any other scope. This could make the
                      global root object effectively unreachable in that scope.
                      There must
                      be some property of this object that you think is not globally
                      applicable.
                      Why? If that is true the same must go for all other objects in the root
                      scope. But this is not about properties or methods, but about the object
                      itself.

                      Objects are only accessable in JS by their naming pointer. different scopes
                      can use the same name for different objects.

                      In the same sense there are no local objects, just local names pointing to
                      objects.

                      If an object is defined in a local scope, however, it would at present be
                      difficult to export the use of such object to another scope that is not a
                      "subscope" of that scope, just because there is no way to move the pointer
                      to that other scope.
                      >It is no matter whether a variable or an object is ment, as it is
                      >only the named pointer that is affected. Only I do not think it is
                      >possible to define a local scope for the default object?
                      >
                      I just don't understand this. Try to explain it with more words.
                      (E.g., do you mean by "default object" what was previously in the
                      thread called "global object"?)
                      No, the default object is the object that does not have to be named in that
                      scope. Perhaps I used that where I ment to say the root object. In JS, it
                      usualy is the same object, but that is implementation.

                      I just object to using the word global here for the root object,
                      becaus it gives the wrong idea, that it is the only object available in all
                      [sub]scopes. All other objects of [= defined in] the root scope have the
                      same possibilities, as long as the name is not reused.

                      --
                      Evertjan.
                      The Netherlands.
                      (Please change the x'es to dots in my emailaddress)

                      Comment

                      • Lasse Reichstein Nielsen

                        #12
                        Re: Javascript Namespace

                        "Evertjan." <exjxw.hannivoo rt@interxnl.net writes:
                        Lasse Reichstein Nielsen wrote on 23 jun 2008 in comp.lang.javas cript:
                        >Default what?
                        >
                        As I wrote in the parallel? posting:
                        >
                        The default object is just the object that is "assumed" if no definite
                        object is specified.
                        >
                        ... when calling that object in the script.
                        >
                        So, in browser js:
                        >
                        window.myValue is the same as myValue,
                        unless in a subscope myValue is redefined
                        [= has a pointer to another object/property/method]
                        The expression "myValue" is a variable expression. It resolves the
                        variable wrt. the current scope. In Javascript it is a lexical scope.
                        If no variable with that name exists in the scope, then Javascript
                        considers the variable to be a global variable. Global variables
                        are properties of the global object.

                        And "window.myValue " is a property lookup on the global object, if
                        "window" resolves to a reference to the global object.

                        As I read your explanation, it seems that you consider "myValue" as
                        being equivalent to "window.myValue " because it is equivalent to a
                        property lookup, with the global object as a "default object" where
                        "myValue" has no explicit object of the property.

                        I consider variable lookup and property lookup to be very different
                        processes. Naming an object that holds the global variables the
                        "global object" is perfectly reasonable to me, and calling it a
                        "default object" is misleading, because "myValue" is not treated
                        as a property lookup on a default object.
                        It would be nice of each subscope could be given a named default subroot
                        object, because now only the myValue of the root scope's and the present
                        scope's myValue are callable,
                        they are the only ones that can be accessed, yes.
                        not the in-between-scope's myValue-s.
                        Indeed. A lexical scope means that you can only access the closest
                        enclosing declaration of a given name. However, being lexically scoped,
                        whoever wrote it would have access to all the code of the scope, and
                        could rename one of the variables if the shadowing wasn't intentional.

                        Yes, just like any other object, but that's why naming that "global" is not
                        right. It is the root object of the root scope.
                        Technically the global object is not in the scope chain. It is
                        accessed only when the scope chain fails to resolve a variable
                        name. Effectively, though, it is almost as if the global object was
                        at the root of all scope chains.
                        Theoretically you could set another object as the default object in the
                        root, so that the original root object is no longer the default.
                        [Perhaps not possible in present day JS, but that is only implementation.]
                        Even in present day Javascript, if you access a function defined in another
                        page, it will have a different global object.
                        Seen as a tree, you could call the object the "global root object",
                        but why shorten that to "global object"? Beter shorten that to "the root
                        object"
                        It's a matter of intent, I guess. The global object holds the global
                        variables. A root object would be at the root of the scope chain.
                        The authors of Javascript had the former perspective.

                        Even the naming pointer to this object can be overwritten to mean another
                        object, either in the root scope or any other scope. This could make the
                        global root object effectively unreachable in that scope.
                        The property "window" of the global object can probably be changed, or
                        shadowed by a local variable. However the global object is always accessible,
                        e.g.,
                        var global = (function(){ret urn this;})();

                        Objects are only accessable in JS by their naming pointer. different scopes
                        can use the same name for different objects.
                        Objects are passed around as references. Those references can be
                        stored in variables or object properties ("naming pointer"), but they
                        can also be the return value of a function.
                        In the same sense there are no local objects, just local names pointing to
                        objects.
                        There are no local objects. The only thing that is local (to a
                        function call) are local variables. That includes arguments
                        (also accessible as properties of the arguments object).
                        If an object is defined in a local scope, however, it would at present be
                        difficult to export the use of such object to another scope that is not a
                        "subscope" of that scope, just because there is no way to move the pointer
                        to that other scope.
                        There are two ways to have values exit a function call: store it in a
                        place that is also accessible outside the call (global variable or
                        local variable in an enclosing scope) or return it as the result
                        of the function. The latter requires no variables/names.
                        No, the default object is the object that does not have to be named in that
                        scope.
                        That's one view of what happens when you access a global variable.
                        However, that view is flawed as the expression "myVariable " is only
                        relative to the global object if it is not a local variable in scope.
                        It's not a property lookup where the target object "isn't named".
                        Perhaps I used that where I ment to say the root object. In JS, it
                        usualy is the same object, but that is implementation.
                        It's specification. The ECMAScript specification states that the
                        global object is where unresolved variables are finally attempted
                        resolved against. I.e., it's what you call the root object of scope
                        based variable resolution.
                        It is also the variables object of global code (code not inside a
                        function body).
                        I just object to using the word global here for the root object,
                        becaus it gives the wrong idea,
                        And I think it is the right idea.
                        that it is the only object available in all [sub]scopes.
                        That's not what "global object" means. But it is true that it is
                        accessible in all scopes. If a function is called directly, and not as
                        a method (referenced as "object.propert y"), then "this" refers to the
                        global object in the method body.
                        All other objects of [= defined in] the root scope have the
                        same possibilities, as long as the name is not reused.
                        Yes, that's just basic scope rules.

                        /L
                        --
                        Lasse Reichstein Nielsen
                        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                        'Faith without judgement merely degrades the spirit divine.'

                        Comment

                        • Henry

                          #13
                          Re: Javascript Namespace

                          On Jun 23, 4:52 pm, Lasse Reichstein Nielsen wrote:
                          Evertjan writes:
                          <snip>
                          >Yes, just like any other object, but that's why naming that
                          >"global" is not right. It is the root object of the root scope.
                          >
                          Technically the global object is not in the scope chain. It is
                          accessed only when the scope chain fails to resolve a variable
                          name. Effectively, though, it is almost as if the global object was
                          at the root of all scope chains.
                          <snip>
                          ECMA 262:-
                          | 10.2 Entering An Execution Context
                          |
                          | ...
                          | 10.2.1 Global Code
                          |
                          | The scope chain is created and initialised to contain the global
                          | object and no others.
                          |
                          | Variable instantiation is performed using the global object as
                          | the variable object and using property attributes {DontDelete }.
                          |
                          | The this value is the global object.

                          So in the global execution context the scope chain consists of one
                          object; the global object.

                          | 13 Function Definition
                          | ...
                          | The production FunctionDeclara tion :
                          | function Identifier ( FormalParameter Listopt ) { FunctionBody }
                          | is processed for function declarations as follows:
                          |
                          | 1. Create a new Function object as specified in section 13.2 with
                          | parameters specified by FormalParameter List, and body specified
                          | by FunctionBody. Pass in the scope chain of the running execution
                          | context as the Scope.
                          | ...

                          Note the "Pass in the scope chain of the running execution context as
                          the Scope". The same step appears in all function creation algorithms
                          except for the - new Function -.

                          | 13.2 Creating Function Objects
                          | Given an optional parameter list specified by FormalParameter List, a
                          | body specified by FunctionBody, and a scope chain specified by
                          Scope,
                          | a Function object is constructed as follows:
                          |
                          | 1. If ...
                          | ...
                          | 19. Set the [[Scope]] property of F to a new scope chain (section
                          | 10.1.4) that contains the same objects as Scope.
                          | 20. Return F.

                          The "Scope" passed becomes the [[Scope]] of the new function object,
                          and then with a Variable object added becomes the scope of the
                          execution context when the function is executed, and so the "running
                          execution context" if any inner functions get created. Thus the global
                          object *is* at the end of all scope chains in javascript.

                          Comment

                          • Thomas 'PointedEars' Lahn

                            #14
                            Re: Javascript Namespace

                            RobG wrote:
                            Thomas 'PointedEars' Lahn wrote:
                            >RobG wrote:
                            >>On Jun 19, 11:04 am, shapper <mdmo...@gmail. comwrote:
                            >>>How to create a namespace in Javascript
                            >>I presume you don't really mean XML namespace. :-)
                            >>The term "namespace" in javascript is sometimes (probably erroneously)
                            >>used to describe a design pattern where functions are added as
                            >>properties of a single object rather than as global functions.
                            >So-called "global functions" are in fact methods of the Global Object, a
                            >single object at that. Your statement is only beginning to make sense if
                            >you say "single *user-defined* object" instead.
                            >
                            [...]
                            The name space object could be any object other than the global
                            object, be it native or host. It need not be a user-defined native
                            object, though I would expect it to be.
                            With host objects, all bets are off.[tm]

                            So it should be a native object. Since the Global Object cannot be
                            considered (using it created the problem in the first place) and other
                            built-in objects do not allow arbitrary augmentation (consider built-in
                            properties with the ReadOnly attribute), it needs to be a user-defined object.


                            PointedEars
                            --
                            Prototype.js was written by people who don't know javascript for people
                            who don't know javascript. People who don't know javascript are not
                            the best source of advice on designing systems that use javascript.
                            -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>

                            Comment

                            • Lasse Reichstein Nielsen

                              #15
                              Re: Javascript Namespace

                              Henry <rcornford@rain drop.co.ukwrite s:
                              Thus the global object *is* at the end of all scope chains in
                              javascript.
                              Indeed, my bad.

                              Note to self: Check spec before presseing "send".
                              /L
                              --
                              Lasse Reichstein Nielsen
                              DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                              'Faith without judgement merely degrades the spirit divine.'

                              Comment

                              Working...