関数はJavaScriptのスコープです。メソッドとして宣言しない限り、関数内から関数を呼び出すことはできません。あなたが望むものであろうとそうでないものであろうと、オブジェクトや奇妙なパターンを使うことを示唆している人もいます...
パブリック/プライベートのメソッドと変数を持つ "クラスのような"関数が必要な場合は、次のようにします。
//Declare your "class"
function Foo(specs) {
//Can do whatever you want with specs
//jQuery uses it to initialize plugin data ;)
//These are examples of private variables
var a = 1;
var b = "b";
//These are examples of public variables
this.aa = 1;
this.bb = "b";
//This is a private method
function Bar_Private(p0,p1) {...}
//This is a public method
this.Bar_Public = function(p0,p1) {...}
}
//Now to use it....
//Create an instance
var myInstance = new Foo();
//Using it
alert(myInstance.a); //Reference Error
alert(myInstance.aa); //Alerts 1
myInstance.Bar_Private(0,1); //Reference Error
myInstance.Bar_Public(0,1); //Calls Bar_Public with 0 and 1 as params
JavaScriptには実際のクラスはありませんが、このパターンを「クラスのような」ものとして記述するのが最も簡単です。
明らかに、公開/非公開のメソッド、オブジェクトなどを好きなだけ追加することができます。
また、このパターンでインスタンスを作成するときには「新しい」オペレータを忘れないでください。そうした場合、「this」はグローバルスペースにバインドされ、他のアプリケーションデータ(名前の衝突)を上書きできます。
これは、幸運を助けることを望む!