1

Ok, I have being trying to find a solution for this for the past 3 hours...

I want to be able to create my own library, accessible function within function with function etc.

Here's what I want to do...

var outer=function()
{
 this.inner=function()
 {
 this.innermost=function()
 {
 alert("innermost");
 }
 } 
}
var outer=new outer;
function start()
{ 
//I want to call it like this but fails!
outer.inner.innermost(); 
}

Now this fails when I try to call the innermost. But if I just have a a function within a function, it works. For example:

var outer=function()
{
 this.inner=function()
 {
 alert("inner");
 } 
}
var outer=new outer; 
function start()
{ 
// this works
outer.inner(); 
}

All the examples I've found only show a function within a function.

I want to be able to create my own library of functions. With an easy way to access them, e.g.:

MyLib.SubLib.SubLib2.function
MyLib.SubLib.SubLib2.property

Any help on the matter would be greatly appreciated. Would I have to learn and use prototypes?

andr
16.1k10 gold badges50 silver badges62 bronze badges
asked Jan 22, 2013 at 1:31
1
  • You may want to look into objects or into self-invoking functions that return objects. Commented Jan 22, 2013 at 1:37

1 Answer 1

3

First of all, this is how you do it:

var outer = function() {
 this.inner = {
 innermost: function() {
 alert("innermost");
 }
 }
}
var outer = new outer;
outer.inner.innermost();

The reason why it didn't work the way you did it is because you define a function inner - so outer.inner is a function. You could do var inner = new (outer.inner)(); and then call inner.innermost(); but that's obviously ugly and not what you want.

answered Jan 22, 2013 at 1:34

1 Comment

Thanks so much that worked, is there a limitation on how deep the functions can go ?, would I be able to do outer.inner.innermost.inneragain(); ?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.