1. 程式人生 > 其它 >P5318 【深基18.例3】查詢文獻

P5318 【深基18.例3】查詢文獻

let const

let 表示申明變數。const 表示申明常量。

  • 常量定義了就不能改了。物件除外,因為物件指向的地址沒變。
  • const在申明是必須被賦值。
  • 兩者都為塊級作用域。
    塊級作用域與函式作用域。任何一對花括號({和})中的語句集都屬於一個塊,在這之中定義的所有變數在程式碼塊外都是不可見的,我們稱之為塊級作用域。函式作用域就好理解了,定義在函式中的引數和變數在函式外部是不可見的。

模組字串``

let name = 'Mike';
let age = 27;
let result = `My Name is ${name},I am ${age+1} years old next year.`;
console.info(result);

解構

陣列模型的解構(Array)

let arr = ['蘋果','西瓜','烏龜'];
let [a,b,c] = arr;
console.info(a);
console.info(b);
console.info(c);


變數有預設值時解析

let [a = 3, b = a] = [];     // a = 3, b = 3,a 與 b 匹配結果為 undefined ,觸發預設值:a = 3; b = a =3
let [a = 3, b = a] = [1];    // a = 1, b = 1,a 正常解構賦值,匹配結果:a = 1,b 匹配結果 undefined ,觸發預設值:b = a =1
let [a = 3, b = a] = [1, 2]; // a = 1, b = 2,a 與 b 正常解構賦值,匹配結果:a = 1,b = 2

物件模型的解構(Object)

let person = {name:'利威爾',age:28,job:'兵長'}
let {name,job} = person;
console.info(name);
console.info(job);

函式

引數預設值

function fn(name,age=17){
 console.log(`My Name is ${name},I am ${age+1} years old`);
}
fn("Amy");

箭頭函式

引數 => 函式體

//es5
var fn_name = function() {

}
//es6
var fn_name = () => {

}
  1. 不需要 function 關鍵字來建立函式
  2. 省略 return 關鍵字
  3. this始終指向函式申明時所在作用域下的this值
var f = (a,b) => {
 let result = a+b;
 return result;
}
f(6,2);  // 8

for of

for of遍歷的是鍵值對中的值
for in遍歷的是鍵值對中的鍵

const arr = ['red', 'green', 'blue'];

for(let v of arr) {
  console.log(v); // red green blue
}

class類

class Student {
  constructor() {
    console.log("I'm a student.");
  }

  study() {
    console.log('study!');
  }

  static read() {
    console.log("Reading Now.");
  }
}

console.log(typeof Student); // function
let stu = new Student();  // "I'm a student."
stu.study();  // "study!"
Student.read(); // "Reading Now."

匯入匯出

匯入improt,引入的變數可以用as起別名
匯出export

/*-----export [test.js]-----*/
let myName = "Tom";
let myAge = 20;
let myfn = function(){
    return "My name is" + myName + "! I'm '" + myAge + "years old."
}
let myClass =  class myClass {
    static a = "yeah!";
}
export { myName, myAge, myfn, myClass }

/*-----import [xxx.js]-----*/
import { myName as n, myAge as a, myfn, myClass } from "./test.js";
console.log(myfn());// My name is Tom! I'm 20 years old.
console.log(a);// 20
console.log(n);// Tom
console.log(myClass.a );// yeah!

export default{}

在一個檔案或模組中,export、import 可以有多個,export default 僅有一個
export default{}可以匯出變數、函式、類。
通過export方式匯出,在import時要加{},export default則不需要{}。
export default向外暴露的成員,可以使用任意變數來接收。

promise

Promise非同步操作有三種狀態:pending(進行中)、fulfilled(已成功)和 rejected(已失敗)。

then

promise.then(onCompleted, onRejected);
一個promise必須提供一個then方法以訪問其當前值、終值和據因。then 方法接收兩個函式作為引數,第一個引數是 Promise 執行成功時的回撥,第二個引數是 Promise 執行失敗時的回撥,兩個函式只會有一個被呼叫。

async/await

async 函式返回一個 Promise 物件,可以使用 then 方法添加回調函式。
await 操作符用於等待一個 Promise 物件, 它只能在非同步函式 async function 內部使用。
await針對所跟不同表示式的處理方式:
Promise 物件:await 會暫停執行,等待 Promise 物件 resolve,然後恢復 async 函式的執行並返回解析值。
非 Promise 物件:直接返回對應的值。

async function helloAsync(){
    console.info(1111);
    return "helloAsync";
  }

console.log(helloAsync())  // Promise {<resolved>: "helloAsync"}

helloAsync().then(v=>{
   console.log(2222);
   console.log(v);         // helloAsync
})

Symbol資料型別

ES6 引入了一種新的原始資料型別 Symbol ,表示獨一無二的值,最大的用法是用來定義物件的唯一屬性名。
ES6 資料型別除了 Number 、 String 、 Boolean 、 Object、 null 和 undefined ,還新增了 Symbol 。

參考:
https://www.cnblogs.com/wang--chao/p/14656871.html

如果這篇文章對你有用,麻煩關注一下本人微信公眾號~