当前位置:网站首页>Understanding of functions

Understanding of functions

2022-06-12 20:34:00 Stand on the bridge and bake cold noodles

What is a function ?

A program structure that holds a piece of reusable code

Several ways to define functions :

  1. Use function Declaration method , The declaration will be advanced . principle :JS Before the engine starts executing , Will scan and use first var Declaration and use function Declared variable name and function name , These two names will be declared in advance , But the assignment remains in place , Will disrupt the normal execution of the program
  2. Use the method of assignment var  Function name =function(){} benefits : Will not be declared ahead of time , It ensures the original execution sequence of the program
  3. use new The way ( Almost no use ). Exposes the nature of functions , Every function The bottom layer is equivalent to new Function, Each function has its own unique memory address , Even two identical functions , The comparison also returns false
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    console.log(a);//undefined
    var a=10;
    console.log(a);//10

    // function fun(){console.log(1)}
    // fun();//2
    // function fun(){console.log(2)}
    // fun();//2

    var fun=function(){
      console.log(1)
    }
    fun();
    var fun=function(){
      console.log(2)
    }
    fun();
  </script>
</body>
</html>
 Running results : 
undefined
10
1
2

原网站

版权声明
本文为[Stand on the bridge and bake cold noodles]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206122029152088.html