JavaScript 基礎
ES6 有什麼新特性?
ES6(ECMAScript 2015)主要新特性
1. let 與 const
取代 var,提供塊級作用域(Block Scope)。
let count = 0;
const MAX = 100;
2. 箭頭函式(Arrow Function)
const add = (a, b) => a + b;
3. 模板字面值(Template Literals)
const name = 'Alice';
console.log(`Hello, ${name}!`);
4. 解構賦值(Destructuring)
const [a, b] = [1, 2];
const { name, age } = { name: 'Bob', age: 25 };
5. 預設參數(Default Parameters)
function greet(name = 'World') {
return `Hello, ${name}!`;
}
6. Spread / Rest 語法
const arr = [1, 2, 3];
const newArr = [...arr, 4, 5]; // spread
function sum(...nums) { // rest
return nums.reduce((a, b) => a + b, 0);
}
7. 類別(Class)
class Animal {
constructor(name) { this.name = name; }
speak() { console.log(this.name); }
}
class Dog extends Animal {}
8. 模組(ES Modules)
// 匯出
export const PI = 3.14;
export default function add(a, b) { return a + b; }
// 匯入
import add, { PI } from './math.js';
9. Promise
fetch('/api/data')
.then(res => res.json())
.catch(err => console.error(err));
10. Symbol
const id = Symbol('id');
11. Map 與 Set
const map = new Map();
const set = new Set([1, 2, 3]);
12. for...of
for (const item of [1, 2, 3]) {
console.log(item);
}
13. 物件簡寫(Object Shorthand)
const name = 'Alice';
const obj = { name, greet() { return 'hi'; } };
14. 迭代器與 Generator
function* gen() {
yield 1;
yield 2;
}
✦ AI 模擬面試
輸入你的答案,AI 即時分析精準度與改進空間
登入後即可使用 AI 評分
