มาตรฐาน ES6, หรือที่รู้จักกันในชื่อ ECMAScript 2015, เป็นเวอร์ชันที่สำคัญของมาตรฐานภาษาโปรแกรม JavaScript ซึ่งได้รับการออกแบบมาเพื่อปรับปรุงและเพิ่มความสามารถให้กับภาษา JavaScript ตัวอย่างของคุณสมบัติหลักๆ ใน ES6 ได้แก่
ES6 ถือเป็นก้าวที่สำคัญในการพัฒนา JavaScript ทำให้ภาษานี้มีความสามารถในการเขียนโปรแกรมที่ซับซ้อนและมีโครงสร้างที่ดีขึ้นอย่างมาก
// let and const let variableLet = "I can change"; const variableConst = "I cannot change"; // Arrow function const addNumbers = (a, b) => a + b; // Template literals let name = "World"; console.log(`Hello, ${name}!`); // Destructuring assignment const object = { a: 1, b: 2, c: 3 }; const { a, b } = object; // Default parameters function greet(name = "Anonymous") { return `Hello, ${name}!`; } // Rest parameters and spread operator function sum(...numbers) { return numbers.reduce((acc, current) => acc + current, 0); } let array = [1, 2, 3]; let arrayCopy = [...array]; // Class with inheritance class Animal { constructor(name) { this.name = name; } speak() { console.log(`${this.name} makes a noise.`); } } class Dog extends Animal { speak() { console.log(`${this.name} barks.`); } } // Promises const promise = new Promise((resolve, reject) => { setTimeout(() => resolve("Promise resolved!"), 1000); }); promise.then((result) => console.log(result)); // Map and Set let map = new Map(); map.set('key1', 'value1'); let set = new Set(); set.add('value1'); // Using all together let myDog = new Dog("Rex"); myDog.speak(); console.log(greet(variableLet)); console.log(addNumbers(a, b)); console.log(sum(...array)); console.log(arrayCopy); console.log(map.get('key1')); console.log(set.has('value1'));
โค้ดนี้รวมฟีเจอร์หลักๆ ของ ES6 เช่น let และ const, arrow functions, template literals, destructuring, default parameters, rest parameters, spread operator, classes และ inheritance, promises, และการใช้งาน Map และ Set. นอกจากนี้ยังมีการนำเอาฟีเจอร์เหล่านี้มาใช้ร่วมกันในส่วนต่างๆ ของโค้ดเพื่อแสดงให้เห็นถึงวิธีการทำงานร่วมกัน