JS का ES6 / 2015
In this page:
let variable = value;
const CONSTANT = value;
const fn = (x) => x;
class ClassName { }
`template ${expr}`
let और const: Block Scoping
let और const ने असली block scoping पेश की (सबसे पास के { } तक सीमित), var के उलझाने वाले function-scoping और hoisting व्यवहार को ठीक करते हुए -- const इसके अलावा reassignment भी रोकता है, programmer के इरादे (यह value नहीं बदलेगी) को खुद code में explicit बनाते हुए।
उदाहरण: let and const: Block Scoping
{
let blockScoped = "inside";
const fixed = "cannot reassign";
console.log(blockScoped, fixed);
}
// console.log(blockScoped); // ReferenceError, not visible out here
Arrow Functions और Template Literals
Arrow functions (x => x * 2) एक छोटा function syntax देते हैं और अपना खुद का this define करने के बजाय अपने surrounding scope से inherit करते हैं, और template literals (Hello ${name}) आपको backticks इस्तेमाल करके expressions को सीधे स्ट्रिंग के अंदर embed करने देते हैं, अजीब string concatenation की जगह लेते हुए।
उदाहरण: Arrow Functions and Template Literals
// Declare the constant `double` as an arrow function taking `x`
// Declare the constant `double` as an arrow function taking `x`
const double = x => x * 2;
// Declare the constant `name`, set to "Sam"
// Declare the constant `name`, set to "Sam"
const name = "Sam";
// Print `double(5), `Hello ${name}`` to the console
// Print `double(5), `Hello ${name}`` to the console
console.log(double(5), `Hello ${name}`);
Destructuring और Default Parameters
Destructuring आपको एक line में किसी array या object से values को individual variables में unpack करने देता है (const { name, age } = person), और default parameters किसी function parameter को caller के कोई value न देने पर अपने आप एक तय value पर fallback होने देते हैं।
उदाहरण: Destructuring and Default Parameters
// Declare the constant `person`, set to `{ name: "Sam", age: 30 }`
// Declare the constant `person`, set to `{ name: "Sam", age: 30 }`
const person = { name: "Sam", age: 30 };
// Destructure {name, age} out of `person`
// Destructure {name, age} out of `person`
const { name, age } = person;
// Define the function `greet` taking `greeting`
// Define the function `greet` taking `greeting`
function greet(greeting = "Hello") { console.log(greeting); }
// Call `greet()`
// Call `greet()`
greet();
// Print `name, age` to the console
// Print `name, age` to the console
console.log(name, age);
Classes और Promises
ES6 का class keyword constructor functions और उनके prototype methods (जो अंदर से JavaScript का मौजूदा inheritance model है) define करने के लिए साफ, ज़्यादा जाना-पहचाना syntax देता है, और Promise object ने asynchronous operations संभालने को standardize किया, उससे पहले के असंगत, ad-hoc callback patterns की जगह लेते हुए।
उदाहरण: Classes and Promises
// Define the class `Animal`
// Define the class `Animal`
class Animal {
constructor(name) { this.name = name; }
}
// Create a new `Promise` instance with `resolve => resolve("done")).then(v => console.log(v)`
// Create a new `Promise` instance with `resolve => resolve("done")).then(v => console.log(v)`
new Promise(resolve => resolve("done")).then(v => console.log(v));
// Print `new Animal("Rex").name` to the console
// Print `new Animal("Rex").name` to the console
console.log(new Animal("Rex").name);
Modules: import और export
ES6 ने एक native module system पेश किया -- export किसी variable, function, या class को दूसरी files के लिए उपलब्ध बनाता है, और import उसे किसी दूसरी file में लाता है -- यह standardize करते हुए कि JavaScript code कई files में कैसे बांटा जाता है, ऐसी चीज़ जिसके लिए language के पास पहले कोई built-in mechanism नहीं था।
उदाहरण: Modules: import and export
// math.js: export function add(a, b) { return a + b; }
// main.js:
import { add } from './math.js';
console.log(add(2, 3));
- यह मान लेना कि class syntax का मतलब है JavaScript को Java या C++ जैसी असली classical inheritance मिल गई -- ES6 classes JavaScript की existing prototype-based inheritance के ऊपर syntax sugar हैं, कोई fundamentally नया object model नहीं।
- नए code में आदतन var इस्तेमाल करना, block-scoping के उन फायदों को miss करते हुए जिनके लिए let और const खासतौर पर introduce किए गए थे।
- यह भूल जाना कि arrow functions this keyword को regular functions से अलग तरीके से handle करती हैं, जो पुराने code को लापरवाही से arrow function syntax में convert करते समय bugs का कारण बन सकता है।
- let और const ने सही block scoping introduce की, ज़्यादातर नए code के लिए var की function-scoping quirks की जगह लेते हुए।
- Arrow functions, template literals, destructuring, और default parameters widely-used ES6 syntax improvements हैं।
- Promises और module system (import/export) ने JavaScript को asynchronous code और code organization के लिए standardized tools दिए।
ES6/ES2015 features लगभग 2016 से release हुए हर browser में natively supported हैं, और current browsers में प्रभावी रूप से universal हैं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: