gpt4 book ai didi

javascript - JavaScript 中的同名函数

转载 作者:数据小太阳 更新时间:2023-10-29 05:46:02 25 4
gpt4 key购买 nike

我试图在一个JS文件上写一个函数,在页面中写另一个同名函数。
我预计会出现错误,但没有出现错误,我只从 JS 文件中获得了要执行的函数。

这怎么可能?即使我在单独的 JS 文件中编写函数,所有内容都呈现在单个 html 文件中。那怎么可能呢?

<script type="text/javascript" language="javascript" src="JScript.js"></script>
<script language="javascript">
function Boo() {
alert("Hai new");
}
</script>

<button onclick="Boo();">Click</button>

在JS文件中

function Boo() {
alert("Hai");
}

最佳答案

One aspect that not many people ever think about with JavaScript is that if you define multiple functions with the same name then the last one defined will be the one that actually runs. JavaScript functions are not polymorphic the way that functions in many other languages are in that JavaScript doesn't care if the actual arguments defined for the functions are different as it can't distinguish between them on that basis. Where in other languages you might have myfunc(oneparm) and myfunc(parmone, parmtwo) as two separate functions with the one that gets run depending on the number of parameters passed, in JavaScript the last one defined will always be the one run regardless of the number of parameters.

http://javascript.about.com/library/blpolyfunc.htm

关于javascript - JavaScript 中的同名函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2911725/

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