JS का ES5 / 2009
In this page:
"use strict";
array.forEach(fn);
JSON.parse(text);
Object.keys(object);
Strict Mode
किसी script या function के ऊपर "use strict" JavaScript के rules के एक ज़्यादा सख्त सेट को opt in करता है -- एक declaration keyword भूल जाने से गलती से global variable बनने जैसी गलतियां पकड़ते हुए, और कुछ उलझाने वाले या असुरक्षित पुराने patterns को disallow करते हुए।
उदाहरण: Strict Mode
"use strict";
try {
x = 5; // undeclared, throws instead of creating a global
} catch (e) {
console.log(e.message);
}
Native JSON Support
JSON.parse() एक JSON-formatted string को एक JavaScript value में बदलता है, और JSON.stringify() एक JavaScript value को एक JSON string में बदलता है -- ES5 के इन्हें standardize करने से पहले, developers JSON data के साथ काम करने के लिए अलग libraries या risky eval() function पर निर्भर रहते थे।
उदाहरण: Native JSON Support
// Declare the constant `obj`, set to `JSON.parse('{"name": "Sam"}')`
// Declare the constant `obj`, set to `JSON.parse('{"name": "Sam"}')`
const obj = JSON.parse('{"name": "Sam"}');
// Print `obj.name` to the console
// Print `obj.name` to the console
console.log(obj.name);
// Print `JSON.stringify(obj)` to the console
// Print `JSON.stringify(obj)` to the console
console.log(JSON.stringify(obj));
Array Iteration Methods
forEach(), map(), filter(), और reduce() -- सभी ES5 additions -- ने arrays को built-in, functional-style iteration methods दिए, कई आम कामों के लिए manual for-loop-based iteration की जगह लेते हुए और आज भी सबसे ज़्यादा इस्तेमाल होने वाले JavaScript methods में से बने रहते हुए।
उदाहरण: Array Iteration Methods
[1, 2, 3].forEach(n => console.log(n));
// Print `[1, 2, 3].map(n => n * 2)` to the console
// Print `[1, 2, 3].map(n => n * 2)` to the console
console.log([1, 2, 3].map(n => n * 2));
// Print `[1, 2, 3].filter(n => n > 1)` to the console
// Print `[1, 2, 3].filter(n => n > 1)` to the console
console.log([1, 2, 3].filter(n => n > 1));
Property Getters और Setters
ES5 ने object literals के अंदर getter और setter syntax पेश किया -- get propertyName() { } और set propertyName(value) { } -- किसी property के read या write व्यवहार को custom code चलाने देते हुए, जैसे कोई derived value calculate करना या किसी assignment को validate करना, फिर भी बाहर से एक साधारण property जैसा दिखते हुए।
उदाहरण: Property Getters and Setters
// Declare the constant `obj`; its value is built below
// Declare the constant `obj`; its value is built below
const obj = {
_value: 10,
get value() { return this._value; },
set value(v) { this._value = v; },
};
// Assign `20` to `obj.value`
// Assign `20` to `obj.value`
obj.value = 20;
// Print `obj.value` to the console
// Print `obj.value` to the console
console.log(obj.value);
ES5 आज भी क्यों मायने रखता है
चूंकि ES5 support अभी इस्तेमाल हो रहे लगभग हर browser में असल में universal है, सिर्फ ES5 features पर निर्भर code (ES6+ के बजाय) अधिकतम compatibility के लिए सबसे सुरक्षित संभव baseline है -- और ES5 समझना उस style में लिखे पुराने tutorials, libraries, और codebases पढ़ते समय मदद करता है।
उदाहरण: Why ES5 Still Matters Today
// Declare the variable `nums`, set to `[1, 2, 3]`
// Declare the variable `nums`, set to `[1, 2, 3]`
var nums = [1, 2, 3];
// Declare the variable `doubled`, set to `nums.map(function (n) { return n * 2; })`
// Declare the variable `doubled`, set to `nums.map(function (n) { return n * 2; })`
var doubled = nums.map(function (n) { return n * 2; });
// Print `doubled` to the console
// Print `doubled` to the console
console.log(doubled);
- यह मान लेना कि ES5-era code (var, function expressions इस्तेमाल करने वाला) किसी तरह "wrong" या unusable होने की हद तक outdated है -- यह पूरी तरह valid, supported JavaScript है, बस एक पुरानी style।
- "use strict" से explicitly strict mode enable करना भूल जाना -- बाद के module syntax के उलट, ES5 strict mode opt-in है, automatic नहीं।
- ES5 के array methods (forEach, map, filter) को नए ES6+ additions से confuse करना -- ये specific methods असल में ES5 से आते हैं, arrow functions और classes से पहले।
- "use strict" strict mode enable करता है, common गलतियां पकड़ते हुए और कुछ error-prone पुराने JavaScript patterns को disallow करते हुए।
- JSON.parse() और JSON.stringify() ES5 में standardize हुए, बिना किसी अलग library के native JSON support देते हुए।
- forEach, map, filter, और reduce जैसे array methods ES5 में introduce हुए, और आज भी JavaScript में सबसे ज़्यादा इस्तेमाल होने वाले methods में से हैं।
ES5 को एक दशक से ज़्यादा समय से active इस्तेमाल हो रहे लगभग हर browser में support मिला है, जिससे यह support मान लेने के लिए सबसे safe baseline बन जाता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: