Home >Web Front-end >JS Tutorial >A brief discussion on the execution of functions and variables with the same name in js

A brief discussion on the execution of functions and variables with the same name in js

高洛峰
高洛峰Original
2017-02-15 17:16:381742browse

The following editor will bring you a brief discussion on the execution of functions and variables with the same name in js. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

After testing, if a function with the same name defined in the same file or in a different js file is not written in a closure form, the function defined later will be executed when called. Even if you write like this, the latter one will be executed and 2 will pop up:

<script type="text/javascript">
 function t(){
   alert(1);
 }
 
 t();
 
 function t(){
   alert(2);
 }
</script>

In addition, the defined variables and css styles are also subject to the latter ones.

But for functions, after testing, writing like this will execute the previous function directly and pop up 1. I don’t know why for the time being.


<script type="text/javascript">
 var t = function(){
   alert(1);
 }
 
 function t(){
   alert(2);
 }

 t();
</script>


The above article briefly discusses the implementation of functions and variables with the same name in js is all the content shared by the editor. , I hope it can give everyone a reference, and I also hope everyone will support the PHP Chinese website.

For more articles on the implementation of functions and variables with the same name in js, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn