18

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");
}
1
  • Because you've redefined the original function? Commented May 26, 2010 at 9:43

6 Answers 6

39

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

Sign up to request clarification or add additional context in comments.

2 Comments

not sure if both of these are same author or not. But if you copy you should recognize original author. forums.asp.net/t/…
@want2learn, its seems both the answers are from about.com JS page. So ideally its not a copy.
4

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.

Comments

2

Case 1: Internal JS has higher priority than External JS, that s why the function written in internal JS override/replaces the other function(with same name) written in external JS file. Example Below:

Internal JS file:

<script type="text/javascript" language="javascript" src="JScript.js"></script>


   <script language="javascript">

     function Boo()
     {
       alert("Hai Internal");//this function will simply override the one 
                             //written in external JS file
     }

   </script>

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

External JS file: JScript.js

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

Case 2: If two function with same name exits in the same file, the one that is declared last(close to the bottom of the file) will override the function above of it. Example below:

<script language="javascript">

  function test() {
    alert("test 1");
  }

  function test() { // this will override/replaces the above function
    alert("test 2");
  }

</script>

2 Comments

Please read How do I write a good answer?. While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others.
@SaeedZhiany, at least he attempted to answer. what you did except criticizing?
0

In JS you can redefine functions. A subsequent function with the same name as another function will overwrite it. (Subject to scope)

Comments

0

Because it gets overwritten by the latest one.

2 Comments

Which is the latest one...the one we write in page or the one in the js file.Which one will be called first..?
@biju Whichever one is last in the page source. In your example, the javascript file is included before your new definition of Boo in the document and therefore overwritten by the new definition in your document.
0

JavaScript is highly forgiving in these aspects (like redeclaring a variable or function). The latest one hides or override the previous ones.

In your case, I assume that the js file was loaded after the function Boo inside the html was parsed. Thus when you click on the button, the definition of Boo in js file is the only Boo available.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.