2.1 函數(shù)
-
函數(shù)的聲明與調(diào)用
聲明函數(shù)有多種方式,常用的有兩種:
第一種方式: function 函數(shù)名( ){ //函數(shù)體; }
注:function:定義函數(shù)的關(guān)鍵字;函數(shù)名后的小括號(hào)中寫參數(shù);如果函數(shù)體中有return語句時(shí),就表示函數(shù)有返回值。
第二種方式: let 函數(shù)名 = function(){ //函數(shù)體; }
注:此兩種方式是完全等效的
// 聲明函數(shù)(1)
function add(){
console.log(1 + 2);
}
// 函數(shù)調(diào)用
add();
// 聲明函數(shù)(2)
let sub = function(){
console.log(2 + 2);
}
// 函數(shù)調(diào)用
sub();
-
參數(shù)與返回值
// 有n個(gè)參數(shù)的函數(shù)(1)
function add(num1,num2){
console.log(num1 + num2);
}
// 傳了兩個(gè)參數(shù)值
add(2,3);
// 有n個(gè)參數(shù)的函數(shù)(2)
let sub = function(num1,num2){//帶參數(shù),帶返回值的函數(shù)
return num1 - num2;
}
console.log(sub(5,1)) ;
-
箭頭函數(shù)
let add = function(){
console.log("hello");
}
add();
let addd = ()=>{ //與上同理,箭頭函數(shù)
console.log("hello");
}
addd();
let adddd = function(a){
return a;
}
console.log(adddd(1));
let dddd = (aa)=>{
return aa;
}
console.log(dddd(2));
//箭頭函數(shù)省略大括號(hào)(只有一句內(nèi)容的時(shí)候才可以省略,超過一句就不能省略)
let aaaa = c => c;
console.log(aaaa(10));
-
作用域
在javaScript中,只有函數(shù)作用域,沒有塊級(jí)作用域;
為了在javaScript中也能使用塊級(jí)作用域,使用let聲明的變量就存在塊級(jí)作用域
var m = 20;
// var沒有塊級(jí)作用域
if(true){
var m = 10;
}
console.log(m);
// var有函數(shù)作用域
function add(){
var n = 20;
}
cosole.log(n);
// let有塊級(jí)作用域
if(true){
let m = 10;
}
console.log(m);
// let也有函數(shù)作用域
function add(){
let n = 10;
}
console.log(n);
// 用var聲明變量,是有變量提升的
console.log(a);
var a = 10;
//相當(dāng)于
// var a;
// console.log(a);
// a = 10;
const聲明變量方式:
const a = 10; //const聲明的變量是常量
console.log(a);
// a = 20; //不能再被修改
2.2 數(shù)組
// 不指定大小(長(zhǎng)度)的數(shù)組
let arr = new Array();
// 指定長(zhǎng)度的數(shù)組
let arr1 = new Array(5);
// 創(chuàng)建數(shù)組時(shí)直接賦值
let arr2 = new Array(10,20,59);
// 創(chuàng)建數(shù)組時(shí)直接賦值(***常用的聲明方式***)
let arr3 = [65,34,56,34,78,29];
console.log(arr3);
JavaScript中的數(shù)組有如下特點(diǎn):
- 數(shù)組下標(biāo)從0開始。
- 雖然創(chuàng)建數(shù)組時(shí),指定了長(zhǎng)度,但實(shí)際上數(shù)組都是可變長(zhǎng)度的,即使超出指定長(zhǎng)度也不會(huì)有任何問題。
- 數(shù)組存儲(chǔ)的數(shù)據(jù)可以是任何類型。
let arr4 = ['hello','li','bai','le','yan','wo','ai','ni']
// 下標(biāo) 0 1 2 3 4
// 最大下標(biāo) = 數(shù)據(jù)長(zhǎng)度 - 1
console.log(arr4[2])
console.log(arr4.length)
for(let i = 0; i < arr4.length; i++){
console.log(arr4[i])
}
// for(let i = 0; i <= arr4.length - 1; i++){
// console.log(arr4[i])
// }
let arr = [21, 43, 12, 54, 32];
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
let arr1 = [12, "ff", true, function() {}, undefined, null];
console.log(arr1);
-
Array數(shù)組內(nèi)置對(duì)象
//push()向數(shù)組中追加一個(gè)元素
let arr = [55, 3, 89, 9, 11];
arr.push(99); //向數(shù)組中追加一個(gè)元素
console.log(arr); //55,3,89,9,11,99
// sort()方法:按照字符串規(guī)則排序
let arr1 = ['SMITH', 'WARD', 'MARTIN', 'CLARK', 'TURNER'];
arr1.sort();
console.log(arr1); //"CLARK", "MARTIN", "SMITH", "TURNER", "WARD"
//所以,當(dāng)對(duì)數(shù)字進(jìn)行排序時(shí),就會(huì)出現(xiàn)問題,此時(shí),可以自定義排序規(guī)則函數(shù)進(jìn)行排序
let arr2 = [55, 3, 89, 9, 11];
arr2.sort(rule);
function rule(num1, num2) {
return num1 - num2;
}
console.log(arr2); //3, 9, 11, 55, 89
// join()方法
let arr3 = [2020, 08, 04];
let result = arr3.join('-');
console.log(result); //2020-8-4
console.log(typeof result);
// splice()方法
//從數(shù)組中間刪除元素:splice(開始位置,刪除元素長(zhǎng)度)
let arr4 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
arr4.splice(2, 3);
console.log(arr4); //運(yùn)行結(jié)果:1,2,6,7,8,9
//在數(shù)組中間插入元素:splice(開始位置,刪除元素長(zhǎng)度,新插入元素… …)
let arr5 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
arr5.splice(2, 0, 31, 32, 33);
console.log(arr5); //運(yùn)行結(jié)果:1,2,31,32,33,3,4,5,6,7,8,9
//替換數(shù)組中某個(gè)元素:splice(開始位置,要替換的元素長(zhǎng)度,替換元素… …)
let arr6 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
arr6.splice(2, 1, 33);
console.log(arr6); //運(yùn)行結(jié)果:1,2,33,4,5,6,7,8,9
-
String 字符串內(nèi)置對(duì)象
//charAt()方法
let str = 'hello';
for(let i=0;i<str.length;i++){
console.log(str.charAt(i));//h e l l o
}
//indexOf()方法
let str = 'hello.js';
console.log(str.indexOf('.')); //5
//substring()方法
let str = 'zhangsan@163.com';
console.log(str.substring(str.indexOf('@')+1)); //163.com
console.log(str.substring(str.indexOf('@')+1,str.indexOf('.'))); //163
//split()方法
let str = '2020-08-04';
let arr = str.split('-');
console.log(arr); //"2020", "08", "04"
-
Date日期內(nèi)置對(duì)象
| 方法 | 描述 |
|---|---|
| getDate( ) | 返回Date對(duì)象一個(gè)月中的一天,其值介于1~31之間 |
| getDay( ) | 返回Date對(duì)象的星期中的一天,其值介于0~6之間 |
| getHours( ) | 返回Date對(duì)象的小時(shí)數(shù),其值介于0~23之間 |
| getMinutes( ) | 返回Date對(duì)象的分鐘數(shù),其值介于0~59之間 |
| getSeconds( ) | 返回Date對(duì)象的秒數(shù),其值介于0~59之間 |
| getMonth( ) | 返回Date對(duì)象的月份,其值介于0~11之間 |
| getFullYear( ) | 返回Date對(duì)象的年份,其值為4位數(shù) |
| getTime( ) | 返回自1970年1月1日至某一時(shí)刻的毫秒值(時(shí)間戳) |
function getCurDate() {
let mydate = new Date(); // 當(dāng)前系統(tǒng)時(shí)間
console.log(mydate);
let year = mydate.getFullYear();
let month = mydate.getMonth() + 1;
let date = mydate.getDate();
month = month < 10 ? '0' + month : month;
date = date < 10 ? '0' + date : date;
let myday = year + '-' + month + '-' + date;
return myday;
}
console.log(getCurDate());
-
Math 數(shù)學(xué)內(nèi)置對(duì)象
| 方法 | 說明 |
|---|---|
| ceil( x ) | 對(duì)數(shù)據(jù)進(jìn)行向上取整 |
| floor( x ) | 對(duì)數(shù)據(jù)進(jìn)行向下取整 |
| round( x ) | 對(duì)數(shù)據(jù)四舍五入為最接近的整數(shù),如果想精確到小數(shù)則使用toFixed() |
| random( x ) | 返回0~1之間的隨機(jī)數(shù) |
| abs( x ) | 返回?cái)?shù)據(jù)的絕對(duì)值 |
| sqrt( x ) | 返回?cái)?shù)據(jù)的平方根 |
| max( x, y ) | 返回兩個(gè)數(shù)據(jù)的最大值 |
| min( x, y ) | 返回兩個(gè)數(shù)據(jù)的最小值 |
// Math.random()生成0~1之間的隨機(jī)小數(shù)
console.log(Math.random() * 10)
// Math.floor(2.5)向下取整
console.log(Math.floor(2.5))
// Math.floor(Math.random() * 10 生成隨機(jī)0`9整數(shù)
console.log(Math.floor(Math.random() * 10))
-
Global內(nèi)置對(duì)象
// parseInt()將字符串轉(zhuǎn)換為整型
let str = '100';
console.log(parseInt(str)+1); //101
// parseFloat() 將字符串轉(zhuǎn)換成浮點(diǎn)型
let str = '100.5';
console.log(parseFloat(str)+1); //101.5
// eval() 將一個(gè)字符串解析為javascript代碼并執(zhí)行
console.log(eval('1+2')); // 3
-
數(shù)組的遍歷/數(shù)組循環(huán)遍歷的方式
let arr = [1, 2, 3, 4];
// 普通for循環(huán)
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
// forEach循環(huán)
arr.forEach(function(item) { //回調(diào)函數(shù)
console.log(item);
})
// for...of循環(huán)
for (let item of arr) {
console.log(item);
}
// map函數(shù)
let a = arr.map(function(item) {
console.log(item);
return item * 2;
})
console.log(a);
// filter函數(shù)
// 對(duì)原來的數(shù)組進(jìn)行篩選過濾,并且返回新的數(shù)組
// 不會(huì)改變?cè)瓉頂?shù)組的內(nèi)容
let b = arr.filter(function(item) {
console.log(item);
//篩選出偶數(shù)
return item % 2 === 0; // === 嚴(yán)格相等
});
console.log(b);
console.log(arr);
// some檢查是否至少有一個(gè)元素滿足條件
let c = arr.some(function(item) {
console.log(item);
return item % 2 === 0; //檢查是否有偶數(shù)
})
console.log(c);
// every檢查是否所有元素都滿足條件
// 得有條件才能遍歷
let d = arr.every(function(item) {
console.log(item);
return item > 0;
});
console.log(d);
// find查找第一個(gè)滿足條件的元素
let e = arr.find(function(item) {
console.log(item);
return item % 2 === 0;
});
console.log(e);
//findIndex判斷數(shù)組中第一個(gè)滿足條件的元素的下標(biāo)
let index = arr.findIndex(function(item) {
console.log(item);
return item % 2 === 0;
})
console.log(inpdex);