Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-22-2012, 04:58 PM   PM User | #1
tricker
New to the CF scene

 
Join Date: Nov 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
tricker is an unknown quantity at this point
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>
tricker is offline   Reply With Quote
Old 11-23-2012, 10:57 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,860
Thanks: 9
Thanked 290 Times in 286 Posts
Dormilich is on a distinguished road
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(); 
__________________
please post your code wrapped in [CODE] [/CODE] tags

Last edited by Dormilich; 11-23-2012 at 11:00 AM..
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:53 AM.


Advertisement
Log in to turn off these ads.