जो मुझे समझ में नहीं आता है यह यह है कि यह विनाश से कैसे संबंधित है।
removeBreakpoint
के भीतर, आप url
, line
, और column
सीधे उपयोग कर सकते हैं। विनाशकारी होता है जब removeBreakpoint
को ऑब्जेक्ट ऑब्जेक्ट के साथ बुलाया जाता है; उस ऑब्जेक्ट की मिलान करने वाली गुण अलग-अलग तर्कों में नष्ट हो जाती हैं। 10, कॉलम:: 20, यूआरएल: 'स्थानीय होस्ट'
विचार यह है कि आप इस समारोह है कि मनमाने ढंग से क्रम में जब तक यह सभी आइटम, यानी {पंक्ति है हो सकता है में एक वस्तु पारित करने की क्षमता की अनुमति है }?
हां, लेकिन इसमें सभी आइटम शामिल नहीं हैं; यदि ऐसा नहीं होता है, तो तर्क किसी ऑब्जेक्ट प्रॉपर्टी से प्रारंभ होता है जो मौजूद नहीं है, तर्क undefined
है (जब तक कोई डिफ़ॉल्ट मान निर्दिष्ट नहीं किया जाता है)।
सरल उदाहरण (Babel के आरईपीएल पर Live Copy with ES5 translation) destructuring प्रदर्शन:
"use strict";
function removeBreakpoint({ url, line, column }) {
console.log("removeBreakpoint:");
console.log("url: " + url);
console.log("line: " + line);
console.log("column: " + column);
}
removeBreakpoint({
url: "the url",
line: "the line",
column: "the column"
});
removeBreakpoint({
url: "the url",
line: "the line"
});
आउटपुट:
removeBreakpoint:
url: the url
line: the line
column: the column
removeBreakpoint:
url: the url
line: the line
column: undefined
यदि हां, तो क्या
function removeBreakpoint(params) {
// ...
}
की तरह कुछ से अधिक लाभ है
जहां परम यूआरएल, रेखा, और कॉलम के साथ एक वस्तु है?
सिंटेक्टिक चीनी। विकल्प ऑब्जेक्ट्स को स्वीकार करने के लिए नया वाक्यविन्यास एक संक्षिप्त पैटर्न स्वचालित करने, अधिक संक्षिप्त और घोषणात्मक है। यह विशेष रूप से स्पष्ट है जब आप मूलभूत मूल्यों के साथ संयोजित (Live Copy) है:
"use strict";
function removeBreakpoint(
{ // <= { starts destructuring arg
url = "url default", // <= Default for `url`
line = "line default", // <= ...for `line`
column = "column default" // <= ...for `column`
} // <= } ends destructuring arg
= {} // <= Default for the options object iself
) { // (see notes after the code block)
console.log("removeBreakpoint:");
console.log(url);
console.log(line);
console.log(column);
}
removeBreakpoint({
url: "the url",
line: "the line",
column: "the column"
});
removeBreakpoint({
url: "the url",
line: "the line"
});
removeBreakpoint();
आउटपुट:
removeBreakpoint:
the url
the line
the column
removeBreakpoint:
the url
the line
column default
removeBreakpoint:
url default
line default
column default
ऊपर, यहां तक कि विकल्पों में वस्तु ही वैकल्पिक है, यही वजह है कि पिछले कॉल काम करता है :
removeBreakpoint();
हम एक डिफ़ॉल्ट विकल्प के लिए खुद को आपत्ति है कि कॉल विफल रहे हैं क्योंकि हम संपत्ति को पढ़ने का प्रयास होगी नहीं दिया था तो का। कभी-कभी आप इसे चाहते हैं, और इसलिए आप समग्र विकल्प बंद कर देंगे। अन्य बार आप नहीं करते हैं।
साइड नोट: डिफ़ॉल्ट विकल्प के कुछ हिस्सों पर आपत्ति है और यह भी, अलग से, पूरे विकल्प होता है कि आप अलग चूक पर कि क्या एक विकल्प वस्तु दिया गया था आधार पर हो सकता है जहां एक दिलचस्प स्थिति पर आपत्ति की क्षमता है, लेकिन बनाम कोई विकल्प सब पर दी जा रही वस्तु एक विशिष्ट विकल्प नहीं था, सब एलान के तौर पर किया: Live Copy
"use strict";
function test(
num,
{
foo = "foo default",
bar = "options given without bar"
} = {bar: "options not given at all"}
) {
console.log(num + ": foo = " + foo + ", bar = " + bar);
}
test(1, {foo: "foo value", bar: "options given with bar"});
test(2, {bar: "options given with bar"});
test(3, {});
test(4);
आउटपुट:
1: foo = foo value, bar = options given with bar
2: foo = foo default, bar = options given with bar
3: foo = foo default, bar = options given without bar
4: foo = foo default, bar = options not given at all
"एक ऑब्जेक्ट ... जो मनमाने ढंग से क्रम में हो सकता है जब तक कि इसमें सभी आइटम शामिल हों" ऑब्जेक्ट्स के पास ऑर्डर नहीं है, और आपके पास सभी आइटम नहीं हैं। कोई सत्यापन या प्रसंस्करण नहीं होता है। लाभ यह है कि फ़ंक्शन बॉडी संदर्भ 'params.url',' params.line', 'params.column' रखने के बजाय, आप सीधे' url', 'line' और' कॉलम' का संदर्भ दे सकते हैं। – Barney
मुझे समझ में आता है। – fox
@ बरनी: ऑब्जेक्ट गुणों में ** ES5 ** में ऑर्डर नहीं है। [वे ** ES6 में ** करते हैं] (https://people.mozilla.org/~jorendorff/es6-draft.html#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys)। –