调用 JavaScript 函数的正确方法是什么?

发布于 2025-01-03 05:16:25 字数 250 浏览 0 评论 0原文

在以下代码中,调用函数 writeMessage 时不使用括号。然而它工作得很好,但是这是在 javaScript 中调用函数的正确方法还是最好将括号与 writeMessage() 一起使用。

window.onload = writeMessage;

function writeMessage()
{
    document.write("Hello World");
}

In the following code, the function writeMessage is called without parenthesis. However it works fine but Is it a correct way of function calling in javaScript or its better to use parenthesis along with writeMessage().

window.onload = writeMessage;

function writeMessage()
{
    document.write("Hello World");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福丶如此 2025-01-10 05:16:25

window.onload = writeMessage; 不是一个调用 - 它是一个赋值。您将 writeMessage 函数指定为 window 对象的 onload 字段。实际调用(在内部)作为 window.onload() 执行,在您的情况下相当于 writeMessage()

window.onload = writeMessage; is not a call - it's an assignment. You assign the writeMessage function as the onload field of the window object. The actual call is performed (internally) as window.onload() which is equivalent to writeMessage() in your case.

卷耳 2025-01-10 05:16:25

在下面的代码中,调用函数 writeMessage 时不带括号。

事实上,事实并非如此。该代码

window.onload = writeMessage;

调用该函数。它将函数分配给windowonload 属性。在浏览器中加载页面的过程的一部分是在加载过程完成后触发分配给该属性(如果有)的函数。

如果您编写的

window.onload = writeMessage();

内容是调用 writeMessage 并将调用的结果分配给 window.onload,就像 x = foo();


请注意,您实际引用的代码(在页面加载时执行 document.write)将清除刚刚加载的页面并将其替换为文本“Hello world”,因为当您页面加载完成后调用document.write,它意味着document.open,这会清除页面。 (这里试试;源代码此处。)在现代网页和应用程序中,您几乎从不使用 document.write,但在极少数情况下,它必须在运行的代码中当页面正在加载时(例如,不稍后)。

In the following code, the function writeMessage is called without parenthesis.

Actually, it isn't. The code

window.onload = writeMessage;

does not call the function. It assigns the function to the onload property of window. Part of the process of loading the page in browsers is to fire the function assigned to that property (if any) once the loading process is complete.

If you wrote

window.onload = writeMessage();

what you'd be doing is calling writeMessage and assigning the result of the call to window.onload, just like x = foo();.


Note that the code you've actually quoted, which executes a document.write when the page loads, will wipe out the page that just loaded and replace it with the text "Hello world", because when you call document.write after the page load is complete, it implies document.open, which clears the page. (Try it here; source code here.) In modern web pages and apps, you almost never use document.write, but in the rare cases where you do, it must be in code that runs as the page is being loaded (e.g., not later).

一身骄傲 2025-01-10 05:16:25

() 用于

在编写

window.onload = writeMessage;

执行函数。您实际上设置了一个委托(指向要执行的函数的指针) - 当onload事件发生时。

the () is used to EXECUTE the function

when you write

window.onload = writeMessage;

you actually set a delegate ( pointer to a function to be executed) for which - when the onload event will occour.

幸福不弃 2025-01-10 05:16:25

这已经是正确的了。

您不需要括号,因为您只是将函数存储在 window.onload 中,而不是自己调用它。

That's correct already.

You don't need parenthesis because you're just storing the function in window.onload, not calling it yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文