Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functions with same names in JavaScript

I tried to write a function on a JS file and another function with the same name in the page.
I expected an error but no error came and I got only the function from the JS file to execute.

How is this possible? Even if I write a function in a separate JS file, everything is rendered in a single html file. Then how come it is possible?

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

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

and in the JS file

function Boo() {
  alert("Hai");
}
like image 976
biju Avatar asked May 26 '10 09:05

biju


People also ask

Can you have 2 functions with the same name JavaScript?

JavaScript supports overriding not overloading, meaning, that if you define two functions with the same name, the last one defined will override the previously defined version and every time a call will be made to the function, the last defined one will get executed.

Can different functions have same name?

Yes, variables belonging to different function can have same name because their scope is limited to the block in which they are defined. Variables belonging to different functions in a single code can have same name if we declare that variable globally as shown in the following code snippet below .

Can you have multiple functions with the same name?

Function overloading is the ability to have multiple functions with the same name but with different signatures/implementations. When an overloaded function fn is called, the runtime first evaluates the arguments/parameters passed to the function call and judging by this invokes the corresponding implementation.

What are the 3 types of functions in JavaScript?

There are 3 ways of writing a function in JavaScript: Function Declaration. Function Expression. Arrow Function.


2 Answers

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

like image 186
LiamB Avatar answered Sep 29 '22 22:09

LiamB


Named functions in javascript are more like variables. If you change the value of a variable, no error occurs, the variable simply has a new value. The same can be said of a function in javascript.

like image 27
Paul Butcher Avatar answered Sep 29 '22 21:09

Paul Butcher