Javascript basic grammar (1)

JavaScript is an indispensable scripting language in current web development. js does not need to be compiled to run. It runs on the client side and needs to be parsed and executed through the browser.
1.
The execution order of the written position js code is (executed from top to bottom)
①In the head tag

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    //script 代码
    <script>
        alert("这是head区域的js代码");
    </script>
</head>

②In the body tag

<body>
	//script 代码
    <script>
        alert("这是body区域的js代码");       
    </script>
    <script src="demo.js"></script>//引入外部js代码文件
</body>

③Writing directly on the label is an abbreviated event, so it is also called event attribute.

// onclick单击事件
<input type="button" value="点我呀!" onclick="alert('点我干啥!^6^');">
<button onclick="alert('恭喜你,中 500 万.');">点我呀!</button>

2. The basic syntax
case sensitive, weakly typed language, end with a semicolon
comment

//js代码注释
       //单行注释 (快捷键 Ctrl+/)
       //alert("这是head区域的js代码");
       
       /*
	多行   (快捷键 Ctrl+Shift+/)
	注释
	*/
	
	/**
	 * @param {*} params 
	 * 星 表示参数是任意类型的值
	 */
	function fun(params) {
    
    
	    alert("Hello World");
	}

3. Variable
3.1 naming convention It
can only be composed of letters, numbers, _ (underscore), and $ (dollar sign).
Cannot start with a number.
-(Js will be understood as a minus sign for subtraction operation) cannot appear in the naming, and cannot conflict with keywords.

var 变量名称 = 存储的数据;    (variable 变量)
//js中变量的定义。只要加一个var就行
数值型:var i = 1; var d = 2.35;
字符串:var str = "用心学习";
布尔型:var b = true;

4. Data Type

数值型:number(凡是数字都是数值型,不区分整数和小数)
字符串:string(凡是引号包裹起来的内容全部都是字符串,单引号双引号都可以)
布尔:boolean(truefalse
对象类型:object(特殊取值null
空对象: Null
未定义型:Undefined
未定义型 和 空对象类型 都是只有一个值的数据类型,值分别为undefined和null

4.1 View data type

typeof(value); 或者typeof value;     //返回这个变量的类型. 
//说明 : 同一个变量, 可以进行不同类型的数据赋值.

<script type="text/javascript">
    var a;
    alert(typeof a);  // undefined
    a = 998;
    alert(typeof a); // number
    a = "用心学习";
    alert(typeof a); // string
    a = true;
    alert(typeof a); // boolean
 </script>

5. Custom function

function 函数名(形式参数){
    
    函数体}
//调用函数:函数名(实际参数);

5.1 If the function needs to return a value, use return directly to return, and it will not consider the type of return value like java

<script type="text/javascript">
    // 定义一个函数 : function
    function demo1() {
    
    
        return 666;
    }
    // 调用函数 :
   alert(demo1());//666
</script>

5.2 If the function needs to pass parameters, do not need to specify the type of the parameter, and use the variable directly

<script type="text/javascript">
    // 定义一个函数 : function
    function demo2(a, b) {
    
    
        return a + b;
    }
    // 调用函数 :
    alert(demo2(10, 20));//显示30
</script>

5.3 There are two function names with the same name in js, and the latter will overwrite the previous one

<script type="text/javascript">
    // 定义一个函数 : function
    function demo3(a, b) {
    
    
        alert("调用1...");
    }
   function demo3() {
    
    
       alert("调用2...");
   }
    demo3(10, 20);
    demo3();
    //显示二次  调用2...
</script>

6. Anonymous function
An anonymous function is a function without a name

function(形式参数){
    
    函数体}
调用方式:将匿名函数赋值给一个变量,通过变量名调用函数
定义函数并赋值给变量:var fn = function(形式参数){
    
    函数体}
调用函数:fn(实际参数);
<script type="text/javascript">
    // 匿名函数 : 没有名称的函数
    var func = function(i, u) {
    
    
        alert(i + " love " + u);
    }
    // 调用函数 :
   func("十元", "里美");//显示 十元love里美
</script>
  1. Global function: window
    7.1 conversion function
    Conversion function
全局函数:
parseInt(num);     // 取整,不会四舍五入
Math.round(num);   // 取整,会四舍五入

7.2 Encoding and decoding functions
Encoding and decoding functions

<script>
        var str = "https://www.baidu.com?wd=编码解码函数";
        // encodeURI 编码字符串(资源路径)
        str = window.encodeURI(str);
        document.write(str + "<br />");
        // decodeURI 解码字符串
        str = window.decodeURI(str);
        document.write(str + "<br />");
    </script>

result

Guess you like

Origin blog.csdn.net/asdasd1fdsyrt/article/details/110450892