gpt4 book ai didi

Javascript,调用函数

转载 作者:行者123 更新时间:2023-11-30 07:10:51 25 4
gpt4 key购买 nike

根据以前的帮助,我正在使用这样的东西:

(function (global) {

// your code here

global.myGlobalVar = myVar

}(this));

这对变量很有效,但我该如何为函数做呢?

例如我试过这个:

(function (global) {

function something()
{
// do something, return something
}

global.something()= something();

}(this));

但这不起作用:(

如何让它与函数一起工作?

谢谢!

编辑:

请注意,这是在 html 页面中调用的,首先我这样做:

<script language="Javascript" src="four.js">

然后

<body onload="javascript:something()">

最佳答案

如果你想声明一个函数,你不应该执行它。所以删除 ()

(function (global) {

function something()
{
// do something, return something
}

global.something = something; // something is the variable
// containing the function and
// you store it into global


}(window));

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

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