window.onload = stuff和window.onload = stuff()之间有区别吗? [英] Is there a difference between window.onload = stuff and window.onload = stuff()?

查看:104
本文介绍了window.onload = stuff和window.onload = stuff()之间有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最熟悉Python,有点用C,当我在JS中看到这种语法时,我真的很困惑

I'm most familiar with Python and somewhat with C, and when I see this syntax in JS it really confuses me

function begin () {
    console.log("done did it");
}

window.onload = begin(); // the same output as
window.onload = begin;   // this one

在Python中,一个是传递函数的返回值,另一个是指向函数的指针。 JS中这两个语句的语义是什么?

In Python, one would be passing the return value of the function, the other a pointer to the function. What are the semantics in JS for these two statements?

推荐答案

是的,这有很大的不同。

Yes, there's a very big difference.

window.onload = stuff(); 调用 的东西 function并将其返回值分配给 onload 属性。

window.onload = stuff(); will call the stuff function and assign its return value to the onload property.

window .onload = stuff; stuff 函数分配给属性 onload 。当加载事件发生时,浏览器将调用该函数。功能是对象。当您通过名称​​引用它们而不调用它们时,您指的是该对象。这类似于你所说的函数指针。

window.onload = stuff; assigns the stuff function to the property onload. The browser will call that function when the load event occurs. Functions are objects. When you refer to them by their name without calling them, you're referring to the object. This would be similar to the "function pointer" you were talking about.

这篇关于window.onload = stuff和window.onload = stuff()之间有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆