जावास्क्रिप्ट में गुणों के लिए noSuchMethod फ़ीचर को कैसे कार्यान्वित करें
जावास्क्रिप्ट में, noSuchMethod राइनो और स्पाइडरमंकी जैसे कार्यान्वयन में सुविधा डेवलपर्स को अप्रयुक्त तरीकों के लिए गतिशील व्यवहार को लागू करने की अनुमति देती है। यह सुविधा प्रॉक्सी ऑब्जेक्ट को एक कस्टम संदेश वापस करने या किसी गैर-मौजूद विधि को कॉल करने पर एक विशिष्ट क्रिया करने में सक्षम बनाती है।
हालांकि मानक जावास्क्रिप्ट भाषा में गुणों के लिए कोई प्रत्यक्ष समकक्ष नहीं है, समान का अनुकरण करना संभव है ईसीएमएस्क्रिप्ट 6 प्रॉक्सी का उपयोग कर कार्यक्षमता। ईसीएमएस्क्रिप्ट 6 की रिलीज ने प्रॉक्सी पेश की है, जो एक शक्तिशाली उपकरण है जो आपको प्रॉपर्टी एक्सेस को रोकने और कस्टम व्यवहार को परिभाषित करने की अनुमति देता है। &&&]
एक कस्टम प्रॉक्सी हैंडलर को परिभाषित करें जो "प्राप्त करें" जाल को ओवरराइड करता है:get: function(target, property) { if (property in target) { // Return the property value if it exists return target[property]; } else if (typeof target.__noSuchMethod__ == "function") { // Call the __noSuchMethod__ method with the property name // as the first argument and any additional arguments as the rest return function(...args) { return target.__noSuchMethod__.call(target, property, args); }; } }
get: function(target, property) { if (property in target) { // Return the property value if it exists return target[property]; } else if (typeof target.__noSuchMethod__ == "function") { // Call the __noSuchMethod__ method with the property name // as the first argument and any additional arguments as the rest return function(...args) { return target.__noSuchMethod__.call(target, property, args); }; } }
const proxy = enableNoSuchMethod({ __noSuchMethod__: function(name, args) { console.log(`No such property ${name} accessed with ${args}`); } }); console.log(proxy.someProperty); // Logs "No such property someProperty accessed with []"
अस्वीकरण: उपलब्ध कराए गए सभी संसाधन आंशिक रूप से इंटरनेट से हैं। यदि आपके कॉपीराइट या अन्य अधिकारों और हितों का कोई उल्लंघन होता है, तो कृपया विस्तृत कारण बताएं और कॉपीराइट या अधिकारों और हितों का प्रमाण प्रदान करें और फिर इसे ईमेल पर भेजें: [email protected] हम इसे आपके लिए यथाशीघ्र संभालेंगे।
Copyright© 2022 湘ICP备2022001581号-3