Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Nested method Calling outside

    Hi All,

    I am trying to call method "childMethod" which is a private method and is not available outside the testSingleton object. i tried couple of ways to achieve this but have found the right solution to it. so would be helpful if someone can point me or tell me the right approach to it.

    May be i havn't defined the method perfectly..
    Below is the code for your rerefrence..


    Thanks in Advance,
    Tricker



    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script language="JavaScript" src="jquery.js" type="text/javascript"></script>
    <script type="text/javascript">

    var testSingleton = {
    init: function(){
    testSingleton.myFirstMethod();
    testSingleton.siblingToInit();
    },//init ends here

    myFirstMethod:function(){
    function childMethod(){
    alert('i am a ChildMethod of init');
    }//childMethod ends here
    },//myFirstMethod ends here

    siblingToInit: function(){
    alert('i am sibling of init');
    }//siblingToInit ends here

    }//testSingleton ends here


    $(document).ready(function(){
    testSingleton.init();
    });

    </script>


    </head>

    <body>

    <script type="text/javascript">
    testSingleton.myFirstMethod.childMethod();
    </script>

    </body>
    </html>

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,436
    Thanks
    13
    Thanked 360 Times in 356 Posts
    so what is your objective here, define a private method?

    anyways, the naming of "child method" and "sibling method" does not really make sense. seen verbatim, every method of an object is a sibling method of the other. "child method" would only make sense if inheritance were involved (which isn’t).

    if child method is meant to be that: testSingleton.myFirstMethod.childMethod();, myFirstMethod must be an object (and in this particular case not even a function), which has a childMethod() property. mind that childMethod() does not have a relation to testSingleton then.
    PHP Code:
    var testSingleton = {
        
    myFirstMethod: {
            
    childMethod: function() {}
        }
    };

    // alternately
    var testSingleton = {
        
    myFirstMethod: function() 
        {
            return {
                
    childMethod: function() {}
            }
        }
    };
    // as
    testSingleton.myFirstMethod().childMethod(); 
    Last edited by Dormilich; 11-23-2012 at 12:00 PM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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