前端開發中js方法的解析
阿新 • • 發佈:2022-04-05
Js(三)
1.Math方法
Math.random()後面可以接加減乘除法
2.日期物件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> var data = new Date(); var year = data.getFullYear();var mon = data.getMonth()+1; //月份是從0開始的,0-11,所以需要加上1 var date = data.getDate(); var hour = data.getHours(); var min = data.getMinutes(); var sec = data.getSeconds(); if (sec<10){sec = "0"+sec} //當時間為一位數時變為01、02、03... var day = data.getDay(); document.body.innerText= year+ '年' + mon + '月' + date + '日' + hour + '時'+min+'分'+sec+'秒'+'星期'+day //拼接 //這裡的時間是死的,重新整理一下才變一下,如果要實時的話需要用到定時器 </script> </body> </html>
效果:
2022年4月2日22時9分02秒星期6
3.函式
函式的定義
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> //定義函式 function func() { alert("hello world") } func() //執行函式 func() //呼叫在上面也能執行函式 function func() { alert("hello world") } </script> </body> </html>
也可以用如下方式執行函式,就不用func()呼叫去執行了:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> *{ margin: 0; padding: 0; } div{ height: 40px; background: yellow; margin: 10px; } </style> </head> <body> <div></div> <div></div> <script> var aDiv = document.getElementsByTagName("div"); function func() { alert("hello world") } aDiv[0].onclick = func; //索引 aDiv[1].onclick = func; </script> </body>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> var fn1=function () { //也可以這麼定義,這麼定義的話呼叫就必須在後面,不能在前面 alert("hello world") } fn1() </script> </body> </html>
匿名函式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> // () + - ! ~ +function (){ alert("hello world"); }() //自己呼叫 !function (){ alert("hello world"); }() -function (){ alert("hello world"); }() ~function (){ alert("hello world"); }() (function (){ alert("hello world"); }()) // ()的方式比較多 </script> </body> </html>
傳參
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> //一 function f(x){ alert(x); alert(typeof x); } f('hello') //傳參 //這麼傳參也可以: var a = 'hello' f(a) //二 sum(1,2) //這裡傳幾個都可以,但是後面要幾個,它就會取前幾個 function sum(x,y,z){ alert(z) //如果前面為function sum(x,y),這裡要彈出z,它不會報錯,會彈出undefined alert(x + y + z) //前面sum(1,2)沒有給z,這裡要拼接的話會顯示NaN(非數字) z = z || 18 //前面如果沒有給z,可以通過這種方式給它值 alert(x + y + z) } </script> </body> </html>
返回值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <input type="button" id="btn" value="改變樣式"> <div id="box" >這是一個div</div> <script> //返回值 function f(a,b) { var num = a+b; alert(num) return num } var qiye = f(1,6); //呼叫並傳參 alert(qiye) //改變樣式的小案例 var oDiv = document.getElementById('box'); var oBtn = document.getElementById("btn"); function fnchange(sColor, sSize) { oDiv.style.color = sColor; oDiv.style.fontSize = sSize; } oBtn.onclick= function () { fnchange("red", '30px') } </script> </body> </html>
不定長引數
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> // 全域性作用域和區域性作用域 //在script裡面的--全域性作用域 //在函式裡面的--區域性作用域 //全域性變數 區域性變數 // js塊級作用域的一個概念---es6之前的版本 //不定長引數es5的用法,es5用的比較多 function sum() { //這裡不需要傳值 console.log(arguments.length) console.log(arguments[3]) } sum(1,2,3,4,5,6,7,8) //這裡傳值 //不定長引數es6的用法 function sum1(...args) { console.log(args) } sum1(1,2,3,4,5,6,7,8) </script> </body> </html>
作用域
這裡介紹的是es6之前的
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> //全域性作用域和區域性作用域 //在script裡面的--全域性作用域 //在函式裡面的--區域性作用域 //定義在標籤中的:全域性變數 定義在函式中的:區域性變數 // js塊級作用域的一個概念---es6之前的版本 //就是說你在裡面定義的變數在外面呼叫的話會報錯 var num = 10; function f1(){ var num; function f2() { console.log(num) //輸出結果為undefined,因為沒給值 } f2() } f1() //因為它會往上找一個最近的,即var num,就近原則,只找一個 //如果沒有再繼續找它的父級 </script> </body> </html>
預解析
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> //第一種 console.log(num); // num is not defined //第二種 console.log(num);//undefined var num= 4; //為什麼?在後面解釋 //就相當於 // var num; // console.log(num); // num = 4 //第三種 f() // 11 為什麼f()在前在後都可以? function f() { console.log(11) } // f() //11 //無論f()在前在後都相當於 function f() { console.log(11) } f() //第四種 f() //f is not defined 為什麼? var f = function () { console.log(100) } f() //100 //f()在前就相當於 var f; f() f = function () { //賦值在後 console.log(100) } //f()在後就相當於 var f; //按順序往下排 f = function () { //賦值在後 console.log(100) } f() //js程式碼執行的時候是分成了兩步來進行的, 預解析 和 執行 //什麼是預解析 // js引擎 會把js 裡面所有的var和function 宣告的東西 放到※※當前作用域※※的最前面 //什麼是執行 // 然後執行, 按照程式碼的順序從上向下執行 // //變數預解析--把所有宣告的變數提到當前作用域的最前面,不賦值,其餘的按順序在最後面往下排 </script> </body> </html>
預解析案例解析
案例一
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> var num = 100; func(); function func() { console.log(num) //undefined var num = 20; } // 相當於 var num; function func() { var num; console.log(num) num = 20; } num = 100; func() </script> </body> </html>
案例二
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> var num = 100; function func() { console.log(num) //undefined var num = 20; console.log(num) //20 } func(); //相當於 // var num; // function func() { // var num; // console.log(num) // num = 20; // console.log(num) // } // num = 100 // func() </script> </body> </html>
案例三
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> var a = 10; f1(); function f1() { var b = 8; console.log(a); //undefined console.log(b); //8 var a = "hello world" } // 相當於 var a; function f1() { var b; var a; b = 8; console.log(a); console.log(b); a = "hello world" } a = 10 f1() </script> </body> </html>
案例四※※※
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> f1(); console.log(c); //9 console.log(b); //9 console.log(a); //9 function f1(){ var a = b= c = 9; // var a = b= c = 9; ====> var a = 9; b= 9; c =9; console.log(a); //9 console.log(b); //9 console.log(c); // a is not defined } //※※※※※※※ //用var宣告的是區域性變數,直接賦值的是全域性變數 //※※※※※※※ // 相當於 function f1(){ var a; a = 9; b = 9; c = 9; console.log(a); console.log(b); console.log(c); } f1() console.log(c); console.log(b); console.log(a); </script> </body> </html>
4.定時器
一次性和迴圈定時器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> setTimeout(function (){ //一次性定時器,到什麼時間執行一次什麼事 console.log(1) },5000) //過五秒控制器內列印1 setInterval(function (){ //迴圈執行定時器 console.log(qqq) },1000) //隔一秒列印一下qqq </script> </body> </html>
清除定時器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="box"> 還有 <span id="wrap">10s</span>我們就下課了!! </div> <script> var wrap = document.getElementById('wrap') var num = 10; var times times = setInterval(function () { num --; wrap.innerHTML = num + "s"; //拼接 if (num===0){ //否則會減到-1,-2,-3,-4,-5... clearInterval(times) //清除定時器 window.location.href = 'https://www.baidu.com' //到時間後跳轉到網址 } },1000) </script> </body> </html>