发布于 2026-01-06 0 阅读
0

ES6

ES6

课程学分

  • 免费编程训练营
  • 迪伦·以色列
  • 课程链接:查看

目录

  • 模板字面量
  • 解构
  • 对象字面量
  • 循环
  • 价差运营商
  • 其余运算符
  • 箭头函数
  • 默认参数
  • 数组.includes
  • 令 & const
  • 进出口
  • String.padStart(), String.padEnd()
  • 课程
  • 尾随逗号
  • 异步/等待

模板字面量

const fruit = "🍌";
const milk = "🥛";
const bananaShake = (fruit, milk) => `(${fruit}) Banana (${milk}) Milk shake is ready.`;
console.log(`${bananaShake(fruit, milk)}`);
Enter fullscreen mode Exit fullscreen mode

解构

对象

const purchasedItems = {
  fruit: "🍇",
  vegetable: "🥕",
  bakery: "🍪"
};
const { bakery } = purchasedItems;
console.log(`Hungry? How about try a ${bakery}?`);
Enter fullscreen mode Exit fullscreen mode

数组

const cart = ["🍇", "🥕", "🍪", "🥩"];
const [item] = cart;
console.log(`Cashier scanned ${item} first.`);
Enter fullscreen mode Exit fullscreen mode

对象字面量

const boilEgg = (egg, water, salt) => {
  const recipe = {
    egg,
    water,
    salt
  };
  console.log(`Get a saucepan, Add 3 cups ${recipe.water} and a pinch of ${recipe.salt}. Throw ${recipe.egg} and boil for 10 minutes.`);
}
boilEgg("🥚", "🚰", "🧂");
Enter fullscreen mode Exit fullscreen mode

循环

let bill = 0;
const receipt = [1.99, 13.67, 14.21, 5.27];
for(const item of receipt) {
  bill += item
};
console.log(`Your total bill is $${bill}.`);
Enter fullscreen mode Exit fullscreen mode

价差运营商

const purchasedItems = {
  fruit: "🍇",
  vegetable: "🥕",
  bakery: "🍪"
};
const juiceRecipe = {
  ...purchasedItems,
  kitchen: "sugar",
  device: "blender"
};
console.log(`Trying a ${juiceRecipe.vegetable} juice.`);
Enter fullscreen mode Exit fullscreen mode

其余运算符

const cart = [];
const addItem = (...items) => console.log(`${items} added in the card. Total items in the cart: ${items.length}.`);
addItem("🍎","🥭","🍍","🍌");
Enter fullscreen mode Exit fullscreen mode

箭头函数

const shoppingBag = [
  {id: 1, name: "🥭", type: "fruit"},
  {id: 2, name: "🍎", type: "fruit"},
  {id: 3, name: "🍌", type: "fruit"},
  {id: 4, name: "🥕", type: "vegetable"}
];
const findVegetable = shoppingBag.filter(item => item.type === "vegetable");
findVegetable ? console.log(`${findVegetable.length} vegetable found in the bag.`) : console.log("Forgot to buy vegetables.");
Enter fullscreen mode Exit fullscreen mode

默认参数

const readyToCheckout = (limit = 10) => limit <= 10 ? console.log(`Today, shopping was in budget.`) : console.log(`Today, lot of items were bought.`)
readyToCheckout(23);
Enter fullscreen mode Exit fullscreen mode

数组.includes

const receipt = ["🍎","🥭","🍍","🍌"];
receipt.includes("🍌") ? console.log(`You bought bananas?`) : console.log(`How come there are no bananas?`);
Enter fullscreen mode Exit fullscreen mode

令 & const

let apple;
apple = "🍏";
console.log(`Green Apple: ${apple}`);
apple = "🍎";
console.log(`Red Apple: ${apple}`);
const banana = "🍌";
console.log(`Banana: ${banana}`);
banana = "🌯"; // WRONG
Enter fullscreen mode Exit fullscreen mode

进出口

String.padStart(), String.padEnd()

let apple = "🍏";
let banana = "🍌";
const dozenApples = apple.padStart(12, apple);
const dozenBananas = banana.padEnd(12, banana);
console.log(`Buy dozen apples: ${dozenApples}. Total apples: ${dozenApples.length}`);
console.log(`Buy dozen bananas: ${dozenBananas}. Total bananas: ${dozenApples.length}`);
Enter fullscreen mode Exit fullscreen mode

课程

尾随逗号

函数参数和对象声明后加逗号是可以的,但不建议这样做。

异步/等待

let cart = new Set(["🍇", "🥕", "🍪", "🍪", "🍪", "🍪", "🥩"]);
console.log(`Total unique items in the cart: ${cart.size}`);
Enter fullscreen mode Exit fullscreen mode
文章来源:https://dev.to/tpkahlon/es6-concepts-using-emojis-1f7a