亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 49
粉絲 0
評(píng)論 0
訪問量 49513
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
ES6 基礎(chǔ)語(yǔ)法總結(jié)
超超多喝水
原創(chuàng)
986人瀏覽過

ES6 基礎(chǔ)語(yǔ)法總結(jié)

var let 與 const

  • var 的不足
    1. 可以重復(fù)聲明 如已聲明 var a = 10;還可以聲明 var a = 20;
    2. 不受區(qū)塊限制 如區(qū)塊一中使用了{(lán)var a = 10;} 后面再聲明 var a = 20;最后輸出的是 20
    3. 只能被閉包(函數(shù))限制區(qū)塊
  • 區(qū)別
    1. var 有變量提升 let 跟 const 沒有
    2. let 聲明的變量可以重新賦值,const 聲明的常量不可以
    3. let 可以聲明一個(gè)值為空的變量,const 聲明的常量必須有值
    4. 一般情況下 const 聲明的常量全部大寫
  • 存儲(chǔ)地址區(qū)別

所有聲明的變量、常量 會(huì)放到棧內(nèi)存 每個(gè)內(nèi)存都有一個(gè)地址 每個(gè)地址都是 16 進(jìn)制

比較大的數(shù)據(jù)比如 json 都會(huì)在堆內(nèi)存開辟一塊空間,空間里也有各個(gè)內(nèi)存,也會(huì)有一個(gè)地址,但這個(gè) json 所賦值給的 let 的變量或 const 的常量是仍舊存在于棧內(nèi)存的,他在棧內(nèi)存里存的只是堆內(nèi)存的空間里內(nèi)存的首地址,堆內(nèi)空間內(nèi)存的地址不可變,但是里面的內(nèi)容是可變的

const json

模板字符串

用兩個(gè)`反引號(hào)包裹可以使用${}的形式添加變量,相如換行等無需再用 html 代碼替換,直接輸入即可

  1. let name = "zhangsirui";
  2. let age = 30;
  3. let jsx = `我叫${name}
  4. 我今年${age}歲`;
  5. console.log(jsx);

輸出:

我叫 zhangsirui
我今年 30 歲

箭頭函數(shù)

一般來說函數(shù)都是用 function 聲明,用箭頭函數(shù)可以使用=>代替 function,使代碼變的更簡(jiǎn)潔

  1. function add (a,b){
  2. return a + b;
  3. }
  1. // 用箭頭函數(shù)
  2. let add = (a,b)=>{
  3. return a + b;
  4. };

箭頭函數(shù)使用時(shí)需要注意以下幾點(diǎn)

  • 如果只有一條語(yǔ)句可以不用大括號(hào),且不用大括號(hào)的時(shí)候 return 也必須省略
  1. let add = (a,b)=>a + b;
  • 沒有參數(shù)或多個(gè)參數(shù)時(shí),使用小括號(hào),因?yàn)闆]有參數(shù)的時(shí)候需要拿括號(hào)當(dāng)結(jié)構(gòu),有多個(gè)參數(shù)需要用逗號(hào)隔開,得用括號(hào)把參數(shù)包裹起來
  1. let info = ()=>'info';
  2. let add = (a,b)=>a + b;
  • 只有單個(gè)參數(shù)的時(shí)候可以省略小括號(hào)
  1. let name = name => `我的名字是${name}`;
  • 如果只有一條語(yǔ)句且這條語(yǔ)句是表達(dá)式或 json 對(duì)象,需要加括號(hào)將內(nèi)容包裹起來
  1. let info = () => ({name:"admin",age:30});
  • 箭頭函數(shù)本身沒有 this,他是借用的父級(jí)的 this
  1. let info = () => this;
  2. //可以看到this一直往上指到了window對(duì)象

數(shù)組

for in 與 for of

for in 是遍歷的數(shù)組的索引 for of 是遍歷的數(shù)組的元素值 for in 更適合遍歷對(duì)象 for of 更適合遍歷數(shù)組

  1. // for in
  2. let good = [4, 12, 20, 34, 56, 5, 8, 45, 10];
  3. let goods1 = [];
  4. let sum = 0;
  5. for (n in good) {
  6. if (good[n] >= 10) {
  7. goods1.push(good[n]);
  8. }
  9. }
  1. // for of
  2. let good = [4, 12, 20, 34, 56, 5, 8, 45, 10];
  3. for (n of good) {
  4. if (n >= 0) {
  5. goods1.push(n * 0.5);
  6. sum += n * 0.5;
  7. }
  8. }

filter 過濾器

filter 過濾器 把符合條件的值過濾出來

  1. let good = [4, 12, 20, 34, 56, 5, 8, 45, 10];
  2. let goods1 = good.filter(function (n) {
  3. return n >= 10;
  4. });
  5. // filter過濾器進(jìn)階
  6. let goods1jj = good.filter((n) => n >= 10);

map 映射

map 映射 把每個(gè)元素處理 處理完了把每個(gè)處理后的結(jié)果返回

  1. let good = [4, 12, 20, 34, 56, 5, 8, 45, 10];
  2. let goods1 = good.filter((n) => n >= 10);
  3. let goods2 = goods1.map(function (n) {
  4. return n * 0.5;
  5. });
  6. // map 映射進(jìn)階
  7. let goods2jj = goods1.map((n) => n * 0.5);

reduce 累加器

reduce(function(a,b){},c)

  • a 前一個(gè)元素, b 當(dāng)前元素, c 可選:指定第一個(gè)元素
  • 如果有 c, 那么 a = c,b=數(shù)組內(nèi)第一個(gè)元素
  • 如果沒有 c,那么 a = 數(shù)組內(nèi)第一個(gè)元素,b=數(shù)組內(nèi)第二個(gè)元素
  1. let good = [4, 12, 20, 34, 56, 5, 8, 45, 10];
  2. let goods1 = good.filter((n) => n >= 10);
  3. let goods2 = goods1.map((n) => n * 0.5);
  4. let sum = goods2.reduce(function (a, b) {
  5. return a + b;
  6. });
  7. // reduce進(jìn)階
  8. let sumjj = goods2.reduce((a, b) => a + b);

startsWith 與 endsWith

  • startsWith 判斷字符串是否以某些字符開頭
  • endsWith 判斷字符串是否以某些字符結(jié)尾
  1. let url = ["https://www.baidu.com/", "http://www.baidu.com/", "http://ipnx.cn/"];
  2. url.forEach((item) => {
  3. if (item.startsWith("https")) {
  4. console.log("安全");
  5. } else {
  6. console.log("err:鏈接不安全,暫不支持");
  7. }
  8. });
  9. url.forEach((item) => (item.endsWith("cn") ? console.log("網(wǎng)站支持") : console.log("網(wǎng)站不支持")));

鏈?zhǔn)秸{(diào)用

  1. let good = [4, 12, 20, 34, 56, 5, 8, 45, 10];
  2. let sum = good
  3. .filter((n) => n >= 10)
  4. .map((n) => n * 0.5)
  5. .reduce((a, b) => a + b);

class 類

  • class + 類名聲明
  • 使用 constructor 方法傳入?yún)?shù)
  • 使用 this.name 的方式獲取類里面的成員
  • 聲明方法可以省略 function
  • 使用 class + 子類名 + extends +父類名可以實(shí)現(xiàn)類的繼承
  • 調(diào)用方法使用 new + 類名 + (參數(shù)) 來調(diào)用
  1. class Person {
  2. constructor(name, age, gender) {
  3. this.name = "name";
  4. this.age = age;
  5. this.gender = gender;
  6. }
  7. //聲明方法
  8. say() {
  9. console.log(this.name);
  10. }
  11. }

json 對(duì)象與字符串相互轉(zhuǎn)換

使用 JSON.stringify(json 對(duì)象) 可以把 json 對(duì)象轉(zhuǎn)為字符串,使用 JSON.parse(json 字符串)可以把 json 字符串轉(zhuǎn)為 json 對(duì)象

  1. let a = "aaa";
  2. let b = "bbb";
  3. let c = "ccc";
  4. let d = function () {
  5. console.log("ddd");
  6. };
  7. // 將a更名為e
  8. const obj = { e: a, b, c, d };
  9. console.log(obj);
  10. let str = JSON.stringify(obj);
  11. console.log(str);
  12. let o = JSON.parse(str);

解構(gòu)賦值

  • 數(shù)組的解構(gòu)賦值是從前往后按順序進(jìn)行賦值
  1. let arr = ["one", "two", "three"];
  2. let [a, b, c] = ["one", "two", "three"];
  • 對(duì)象的解構(gòu)賦值是按名稱來進(jìn)行賦值,跟順序沒有關(guān)系
  1. const { name, gender, age, say } = {
  2. name: "admin",
  3. age: 30,
  4. gender: "男",
  5. say() {
  6. return "aaa";
  7. },
  8. };
  • 解構(gòu)數(shù)組中的對(duì)象
  1. const [a, b, c, { x: g, y }, d, e] = ["a", "b", "c", { x: "aaa", y: "bbb" }, "d", "e"];
  2. console.log(a, b, c, g, y, d, e);
  • 解構(gòu)對(duì)象中的數(shù)組
  1. const {
  2. a,
  3. b,
  4. c,
  5. d: [x, y],
  6. e,
  7. } = { a: "a", b: "b", c: "c", d: ["aaa", "bbb"], e: "e" };
  8. console.log(a, b, c, x, y, e);
  • …展開合并參數(shù)
    …在數(shù)組中就是展開,在單值中就是合并
  1. const [a, b, ...c] = [1, 2, 3, 4, 5, 6, 7];
  2. console.log(a, b, c);
  1. function add(...args) {
  2. return args.reduce((a, b) => a + b);
  3. }
  4. console.log(add(1, 2, 3, 5, 4, 56, 4, 5, 10));
  5. console.log(add(...c));

Module 模塊化編程

one.js

  1. // 使用export將變量跟函數(shù)單個(gè)導(dǎo)出
  2. export let a = 10;
  3. export function add(a, b) {
  4. return a + b;
  5. }
  6. console.log("one.js");

two.js

  1. let b = 20;
  2. function add(a, b) {
  3. return a + b;
  4. }
  5. console.log("two.js");
  6. // 使用export將變量跟函數(shù)打包導(dǎo)出
  7. export { b, add };

three.js

  1. let d = 40;
  2. function add(a, b) {
  3. return a + b;
  4. }
  5. // 進(jìn)行缺省導(dǎo)出,一個(gè)模塊只能有一個(gè)缺省導(dǎo)出
  6. export default function (...args) {
  7. return args.reduce(function (a, b) {
  8. return a + b;
  9. });
  10. }
  11. // 導(dǎo)出的時(shí)候給函數(shù)或者變量更名
  12. export { d, add as fun1 };

four.js

  1. let e = 40;
  2. function add(a, b) {
  3. return a + b;
  4. }
  5. export default function (...args) {
  6. return args.reduce(function (a, b) {
  7. return a + b;
  8. });
  9. }
  10. export { e, add as fun2 };

index.js

  1. // 使用解構(gòu)賦值的方法 用import將變量及函數(shù)導(dǎo)入
  2. import { a, add } from "./one.js";
  3. // 導(dǎo)入的時(shí)候給變量或函數(shù)更名
  4. import { b, add as sum } from "./two.js";
  5. // 使用*as可以將所有內(nèi)容導(dǎo)入,并存放到一個(gè)變量中
  6. import * as three from "./three.js";
  7. import { e, fun2 } from "./four.js";
  8. let c = 30;
  9. console.log("########");
  10. console.log(add(a, b));
  11. console.log("########");
  12. console.log(sum(b, c));
  13. console.log("########");
  14. // 從導(dǎo)入的所有內(nèi)容中調(diào)用里面的變量及函數(shù)
  15. console.log(three["fun1"](c, three["d"]));
  16. console.log("########");
  17. console.log(three["default"](a, b, c, three["d"]));
  18. console.log("########");
  19. console.log(fun2(a, e));

index.html

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. <!-- 使用module模塊化編程需要加上type="module才能生效 -->
  9. <script src="index.js" type="module"></script>
  10. </head>
  11. <body></body>
  12. </html>
批改老師:PHPzPHPz

批改狀態(tài):合格

老師批語(yǔ):
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
作者最新博文
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)