gpt4 book ai didi

javascript - 在函数内调用 javascript 函数

转载 作者:搜寻专家 更新时间:2023-11-01 05:00:55 24 4
gpt4 key购买 nike

我见过这样的javascript

var Gallery = function () {

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

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

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

如何在这个 javascript 文件中调用 helloworlds?

我试过了

  • helloworld1();
  • Gallery.helloworld1();
  • 图库().helloworld1();

但它们似乎不起作用。

此外,是否有任何理由让我以上述方式编写我的 javascript,而不是仅仅使用

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

最佳答案

也许你想要

var Gallery = {

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

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

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

Gallery.helloworld2();

var Gallery = function () {

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

this.helloworld2 = function () {
alert('hey2');
}

this.helloworld3 = function () {
alert('hey3');
}
};

new Gallery().helloworld2();

Also, are there any reasons why I should be writing my javascript in the above manner?

它为函数命名。与画廊有关的一切都在 Gallery 对象中。

关于javascript - 在函数内调用 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23183431/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com