क्या शुद्ध फ़ंक्शन के अंदर लॉनाश का उपयोग _.map
इनपुट मान के लिए करना ठीक है या क्या मैं केवल मूल Array.map
का उपयोग कर सकता हूं जो धीमा है?
उदाहरण के लिए:
let shortcuts = _.map(state.shortcuts, (shortcut: any) => {
switch(shortcut.page){
case "Transfers": return tassign(shortcut, { badge: action.payload.transfers });
case "Payments": return tassign(shortcut, { badge: action.payload.payments });
case "Inbox": return tassign(shortcut, { badge: action.payload.inbox });
case "ConsolidatedPosition": return tassign(shortcut, { badge: action.payload.consolidatedPosition });
default: return shortcut;
}
});
return tassign(state, { shortcuts: shortcuts });
2 जवाब
लोडाश के map
की कोई स्थिति नहीं है और न ही कोई साइड इफेक्ट है, इसलिए यह ठीक है।
एक शुद्ध फ़ंक्शन एक ऐसा फ़ंक्शन है जहां वापसी मूल्य केवल इसके इनपुट मानों द्वारा निर्धारित किया जाता है, बिना किसी साइड इफेक्ट के।
लोडाश का नक्शा Array.prototype.map() के समान काम करता है जो इस सरणी में प्रत्येक तत्व पर दिए गए फ़ंक्शन को कॉल करने के परिणामों के साथ एक नई सरणी बनाता है।
आम तौर पर फ़ंक्शन को शुद्ध रखने के लिए, हमेशा एक नया मान लौटाएं और उस फ़ंक्शन के दायरे से बाहर कोई अन्य संचालन न करें, जैसे डीओएम संबंधित संचालन आदि।
शुद्ध कार्य और रिडक्स: http://redux.js.org/docs/introduction/ThreePrinciples .html