Это зависит от того, как вы хотите настроить первую функцию и как вы хотите получить к ней доступ. По сути, третий метод является частным по отношению к первому. Сначала вам понадобится публичный метод для вызова третьего. Этот метод будет вызван вторым.
Существуют различные способы сделать это, один из которых приходит на ум, это ...
Edit: я назвал параметры немного лучше, так что это не "param" снова и снова.
function first()
{
// define first's private
var third = function(third_param)
{
alert(third_param);
}
// define first's public that calls the private
this.callThird = function (call_third_param)
{
third(call_third_param);
}
}
function second ()
{
// get an instance of first
var temp = new first();
// call the public method on it
temp.callThird('argument');
}
Если вам не важно, чтобы третьи были частными, вы можете сделать
function first() { }
// public method
first.prototype.third = function(third_param)
{
alert(third_param);
}
function second ()
{
// get an instance of first
var temp = new first();
// call the public method on it
temp.third('argument');
}
или, таким образом, это также не использует рядовых
function first()
{
// the "this", makes it public
this.third = function(third_param)
{
alert(third_param);
}
}
function second ()
{
// get an instance of first
var temp = new first();
// call the public method on it
temp.third('argument');
}
Если вы пытаетесь просто сделать что-то в пространстве имен, вы можете сделать это (также нет рядовых)
// define an object and name it first
var first =
{
// give first a public variable but make it a function with an arg
third : function(third_param)
{
alert(third_param);
}
}
function second ()
{
// call the third method on the first variable
first.third('argument');
}
Но, вероятно, лучший способ - через пространство имен
var first = (function () {
// define first's private function, store it in 'third', a private variable
var third = function (third_param) {
alert(third_param);
};
// return this object to the 'first' variable
return { // define first's public that calls the private
'callThird': function (call_third_param) {
third(call_third_param);
}
};
} ()); // this line runs the function, closure'ing over the privates and
// returning an object (that has access to the privates) to the 'first' variable
function second () {
// usage:
first.callThird("Hello World!");
}