JS,Javascript加载与函数执行过程

Js,Javascript加载与函数执行过程

test.html

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>demo</title>
	<script type="text/javascript">
    	function hello(){
    		alert("hello 1");
    	}
    	hello(); // hello 2
    	function hello(){
    		alert("hello 2");
    	}
    	hello(); // hello 2
    </script>
    <script type="text/javascript">
		hello(); // hello 2
	</script>


    <script type="text/javascript">
    	function hello(){
    		alert("hello 3");
    	}
    	hello(); // hello 4
    	function hello(){
    		alert("hello 4");
    	}
    	hello(); // hello 4
    </script>

    <script type="text/javascript">
		hello(); // hello 4
	</script>
</head>

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


<script type="text/javascript">
	hello(); // hello js2
</script>
</html>

test.js

function hello(){
	alert("hello js1"); 
}
hello(); // hello js2

function hello(){
	alert("hello js2"); 
}
hello(); // hello js2

小结

1.js中可以允许重复的函数

2.函数式一块一块执行的以script作为块标记

3.同一个块中,如果有同名函数,以后一个为准

4.引入js文件也就是相对于一个大的script块

5.如果在页面底部执行js,相对于以最后一个函数作为执行函数

原文地址:https://www.cnblogs.com/jiqing9006/p/5957361.html