在函数中调用javascript函数

在函数中调用javascript函数,javascript,function,nested,encapsulation,Javascript,Function,Nested,Encapsulation,我见过类似这样的javascript var Gallery = function () { var helloworld1 = function () { alert('hey1'); } var helloworld2 = function () { alert('hey2'); } var helloworld3 = function () { alert('hey3'); } }();

我见过类似这样的javascript

var Gallery = function () {

    var helloworld1 = function () {
        alert('hey1');
    }

    var helloworld2 = function () {
        alert('hey2');
    }

    var helloworld3 = function () {
        alert('hey3');
    }
}();
我如何在这个javascript文件中调用HelloWorld

我试过了

  • helloworld1()
  • Gallery.helloworld1()
  • Gallery().helloworld1()
但它们似乎不起作用

还有,我为什么要以上述方式编写javascript,而不仅仅是使用

function helloworld() {
    alert('hey1');
}

函数中声明的符号仅在该函数内可见(以及在该函数内定义的其他函数内)。这同样适用于使用
var
function
声明的符号。因此,在第一个示例中,您不能从外部函数访问这些函数

通过让外部容器函数返回一个对象,并将函数作为属性值,可以“导出”函数

至于函数应该用
function
还是
var
来声明,这是个人风格的问题。我(个人)喜欢
function
,但我知道有许多非常有天赋的JavaScript程序员使用
var
初始化。

也许你想要

var Gallery = {

    helloworld1: function () {
        alert('hey1');
    },

    helloworld2: function () {
        alert('hey2');
    },

    helloworld3: function () {
        alert('hey3');
    }
};

Gallery.helloworld2();


还有,我为什么要以上述方式编写javascript


它为函数命名空间。与库有关的所有内容都在
库对象中。

如果您在引用的代码中注意到,库函数后面的括号。这些括号用于立即调用函数。因此,只要代码运行,就会调用该函数。并且该特定函数正在分配3 he将世界函数分配给各自的变量。但由于第一个答案所述的原因,无法访问这些函数。因此,我不确定该代码的目的。

关于您的第一个问题。变量库被分配给函数返回的值,这基本上是未定义。 无法执行Gallery.helloworld(),因为..Gallery未定义。 helloworld1()无法正常工作,因为它将在未再次定义的全局对象上查找helloworld1函数。 这是因为您在函数范围内定义了这些函数

你需要这样做才能实现你想要做的事情

var Gallery = function(){
    return {
        helloworld1:function(){alert('hey1')},
        helloworld2:function(){alert('hey2')},
        helloworld3:function(){alert('hey3')}
    }
}();
这会将Gallery分配给一个对象,该对象将所有三个函数都作为其属性。通过这种方式,您可以调用.Gallery.helloworld1(),它将打印警报

这是一种防止对象进一步修改的方法。 你可以这样做

var Gallery = function(){
    var realGallery = {no_of_pictures:1};
    return {
        getNumberOfPictures:function(){return realGallery.no_of_pictures;},
        increasePictures:function(){realGallery.no_of_pictures += 1;},
        decreasePictures:function(){realGallery.no_of_pictures -= 1;}

    }
}();
这样,任何人都不能在realGallery对象上直接设置no_of_图片。它们只能递增或递减。 关于你的第二个问题。使用globals通常被认为是不好的做法,因为任何人都可以篡改它们。 假设您定义了
函数helloworld(){alert('hey1');}
有人可以像这样轻易地重新定义他的世界。
函数helloworld(){alert('gowaynow!!');}


因此,有人可以很容易地用一些小技巧完全破坏您的应用程序。这就是为什么在javascript中globals被认为是一种不好的做法。

这里的关键是您有一个自动执行的函数,它不返回值,因此您无法从其中访问任何内容,无论它是否使用
var或

这种编码风格的目标是避免用大量变量和函数污染全局空间。所有内容都封装在Gallery函数中。使用这种风格,您不会试图从外部访问任何内容。它就像一个引导函数,执行一组过程,然后完成,而无需再给里面的人打电话

如果要从外部访问,首先需要删除自执行部分,然后声明公共属性或私有属性的公共指针:

var Gallery = function () {

    var helloworld1 = function () {
        alert('hey1');
    }

    var helloworld2 = function () {
        alert('hey2');
    }

    var helloworld3 = function () {
        alert('hey3');
    }

    this.helloworld1 = helloworld1;
    this.helloworld2 = helloworld2;
    this.helloworld3 = helloworld3;

};

var g = new Gallery();
g.helloworld1();
如果要让函数本身返回,则可以访问内部(前提是方法/属性通过
this
公开):

var Gallery = function () {

    this.helloworld1 = function () {
        alert('hey1');
    }

    return this;
}();

console.log(Gallery.helloworld2);
var Gallery = function () {

    this.helloworld1 = function () {
        alert('hey1');
    }

    return this;
}();

console.log(Gallery.helloworld2);