SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript namespaces

    I have a Javascript function inside a namespace as follows:

    var slider = function() {
    /* ==== private methods ==== */
    function showAlert() {
    alert('123');
    }
    }

    If I want to call the function showAlert() from outside the namespace, how should I call it?

    I've tried calling it by using slider.showAlert() but it doesn't work and I receive an error message that showAlert is not a function.

    Any ideas? Everything I've found on Google talks about how to setup namespaces and call the functions from inside the namespace, but I am having trouble finding any information about calling a function located inside a namespace externally. Is this even possible?

    Thanks,
    Phil

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    JavaScript doesn't currently have proper namespaces. It appears that you're looking for something like this,
    Code JavaScript:
    var slider = {
        showAlert: function () {
            alert('123');
        }
    };
     
    slider.showAlert();
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what you have done is not a namespace, it is a function within a function. These are usually treated as classes.

    so to call your example you would have to do the following:

    Code javascript:
    var s = new slider();
    s.showAlert();
    // or
    new slider().showAlert();

    to create a name space effect you can use something similar to json (i can't recall the actual correct name.)

    Code xhtml:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Untitled Document</title>
            <link rel="stylesheet" type="text/css" media="screen" />
            <style type="text/css"></style>
            <script type="text/javascript">
                var slider = {
                    showAlert : function() {
                        alert('123');
                    },
                    someClass : function() {
                        this.property = 1;
                        this.method = function() {
                            alert('someClass.method called');
                        };
                    }
                };
     
                window.onload = function() {
                    slider.showAlert(); // static
                    var instance = new slider.someClass(); // creates an instance of someClass
                    instance.method(); // calls a non-static method from the isntance of someClass
                }
            </script>
        </head>
        <body>
        </body>
    </html>


  4. #4
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help! Works now.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •