खोज…


विधि जंजीर

मेथड चाइनिंग एक प्रोग्रामिंग रणनीति है जो आपके कोड को सरल बनाती है और इसे सुशोभित करती है। मेथड चेनिंग यह सुनिश्चित करके किया जाता है कि किसी वस्तु पर प्रत्येक विधि उस वस्तु के एक तत्व को वापस करने के बजाय, पूरी वस्तु को लौटा दे। उदाहरण के लिए:

function Door() {
    this.height = '';
    this.width = '';
    this.status = 'closed';
}

Door.prototype.open = function() {
    this.status = 'opened';
    return this;
}

Door.prototype.close = function() {
    this.status = 'closed';
    return this;
}
Door.prototype.setParams = function(width,height) {
    this.width = width;
    this.height = height;
    return this;
}

Door.prototype.doorStatus = function() {
    console.log('The',this.width,'x',this.height,'Door is',this.status);
    return this;
}

var smallDoor = new Door();
smallDoor.setParams(20,100).open().doorStatus().close().doorStatus();

नोट में प्रत्येक विधि कि Door.prototype रिटर्न this है, जो इस बात का पूरा उदाहरण के लिए संदर्भित करता Door वस्तु।

चेनेबल ऑब्जेक्ट डिज़ाइन और चेनिंग

चेनिंग एंड चैनेबल एक ऑब्जेक्ट मेथडोलॉजी है जिसका उपयोग ऑब्जेक्ट व्यवहारों को डिजाइन करने के लिए किया जाता है ताकि ऑब्जेक्ट फ़ंक्शंस पर कॉल स्वयं के संदर्भ में लौटें, या किसी अन्य ऑब्जेक्ट, अतिरिक्त फ़ंक्शन कॉल तक पहुंच प्रदान कर कॉल स्टेटमेंट को एक साथ कई कॉल को वेरिएबल होल्डिंग को संदर्भित करने की आवश्यकता के बिना अनुमति देता है। वस्तु / एस।

जिन वस्तुओं को जंजीर में डाला जा सकता है, उन्हें चेनेबल कहा जाता है। यदि आप किसी ऑब्जेक्ट को श्रृंखला योग्य कहते हैं, तो आपको यह सुनिश्चित करना चाहिए कि सभी लौटे ऑब्जेक्ट / प्राइमेटिव सही प्रकार के हों। यह केवल करने के लिए अपने chainable वस्तु के लिए एक समय लगता है सही संदर्भ (जोड़ना भूल करने के लिए आसान नहीं लौट return this ) और व्यक्ति अपने एपीआई का उपयोग कर से बचने के विश्वास और श्रृंखलन खो देंगे। चेनेबल ऑब्जेक्ट्स सभी या कुछ भी नहीं होने चाहिए (चेनेबल ऑब्जेक्ट नहीं हैं भले ही पार्ट्स हों)। एक वस्तु को चेनेबल नहीं कहा जाना चाहिए यदि केवल इसके कुछ कार्य हैं।

ऑब्जेक्ट चेनेबल होने के लिए डिज़ाइन किया गया है

function Vec(x = 0, y = 0){
    this.x = x;
    this.y = y;
    // the new keyword implicitly implies the return type 
    // as this and thus is chainable by default.
}
Vec.prototype = {
    add : function(vec){
        this.x += vec.x;
        this.y += vec.y;
        return this; // return reference to self to allow chaining of function calls
    },
    scale : function(val){
        this.x *= val;
        this.y *= val;
        return this; //  return reference to self to allow chaining of function calls
    },
    log :function(val){
        console.log(this.x + ' : ' + this.y);
        return this;
    },
    clone : function(){
        return new Vec(this.x,this.y);
    }
}

चैनिंग उदाहरण

var vec = new Vec();
vec.add({x:10,y:10})
    .add({x:10,y:10})
    .log()             // console output "20 : 20"
    .add({x:10,y:10})
    .scale(1/30)
    .log()             // console output "1 : 1"
    .clone()           // returns a new instance of the object
    .scale(2)          // from which you can continue chaining
    .log()

वापसी प्रकार में अस्पष्टता पैदा न करें

सभी फ़ंक्शन कॉल उपयोगी चेनेबल प्रकार नहीं लौटाते हैं, और न ही वे हमेशा स्वयं का संदर्भ देते हैं। यह वह जगह है जहाँ नामकरण का सामान्य ज्ञान उपयोग महत्वपूर्ण है। उपरोक्त उदाहरण में फ़ंक्शन कॉल .clone() असंदिग्ध है। अन्य उदाहरण हैं .toString() का अर्थ है कि एक स्ट्रिंग वापस आ गई है।

एक श्रृंखला योग्य वस्तु में एक अस्पष्ट फ़ंक्शन नाम का एक उदाहरण।

 // line object represents a line
 line.rotate(1)
    .vec();  // ambiguous you don't need to be looking up docs while writing.

 line.rotate(1)
    .asVec()    // unambiguous implies the return type is the line as a vec (vector)
    .add({x:10,y:10)
 // toVec is just as good as long as the programmer can use the naming 
 // to infer the return type

सिंटेक्स सम्मेलन

चिनिंग करते समय कोई औपचारिक उपयोग सिंटैक्स नहीं है। यदि लाइन छोटी है या नई लाइन पर चेन के लिए कन्वेंशन है तो नई लाइन पर डॉट के साथ संदर्भित ऑब्जेक्ट से एक टैब को इंडेंट करने के लिए। अर्धविराम का उपयोग वैकल्पिक है लेकिन श्रृंखला के अंत को स्पष्ट रूप से दर्शाते हुए मदद करता है।

  vec.scale(2).add({x:2,y:2}).log();  // for short chains

  vec.scale(2)     // or alternate syntax
      .add({x:2,y:2})
      .log();  // semicolon makes it clear the chain ends here

  // and sometimes though not necessary
  vec.scale(2)     
      .add({x:2,y:2})
      .clone()    // clone adds a new reference to the chain
           .log(); // indenting to signify the new reference

  // for chains in chains
  vec.scale(2)     
      .add({x:2,y:2})
      .add(vec1.add({x:2,y:2})  // a chain as an argument 
           .add({x:2,y:2})      // is indented
           .scale(2))
      .log();

  // or sometimes 
  vec.scale(2)     
      .add({x:2,y:2})
      .add(vec1.add({x:2,y:2})  // a chain as an argument 
           .add({x:2,y:2})      // is indented
           .scale(2)
      ).log();   // the argument list is closed on the new line

एक बुरा वाक्यविन्यास

   vec          // new line before the first function call
      .scale()  // can make it unclear what the intention is
      .log();

   vec.          // the dot on the end of the line
      scale(2).  // is very difficult to see in a mass of code
      scale(1/2); // and will likely frustrate as can easily be missed
                  // when trying to locate bugs

असाइनमेंट का बायाँ हाथ

जब आप एक श्रृंखला के परिणाम असाइन करते हैं तो अंतिम रिटर्निंग कॉल या ऑब्जेक्ट संदर्भ सौंपा जाता है।

 var vec2 = vec.scale(2)
                .add(x:1,y:10)
                .clone();   // the last returned result is assigned
                                // vec2 is a clone of vec after the scale and add

उपरोक्त उदाहरण में vec2 को श्रृंखला में अंतिम कॉल से लौटाया गया मान दिया गया है। इस मामले में, कि स्केल और ऐड के बाद vec की एक प्रति होगी।


सारांश

बदलने का लाभ अधिक स्पष्ट कोड है। कुछ लोग इसे पसंद करते हैं और एपीआई का चयन करते समय श्रृंखला को एक आवश्यकता बना देंगे। एक प्रदर्शन लाभ भी है क्योंकि यह आपको अंतरिम परिणाम रखने के लिए चर बनाने से बचने की अनुमति देता है। अंतिम शब्द के साथ कि चेनेबल ऑब्जेक्ट्स का उपयोग पारंपरिक तरीके से किया जा सकता है और साथ ही आप ऑब्जेक्ट चेनेबल बनाकर चेनिंग को लागू नहीं करते हैं।



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow