Computer >> कंप्यूटर >  >> प्रोग्रामिंग >> Javascript

जावास्क्रिप्ट में मेथड चेनिंग

<घंटा/>

चेनिंग मेथड्स, जिसे कैस्केडिंग के रूप में भी जाना जाता है, का अर्थ है किसी वस्तु पर एक के बाद एक विधि को बार-बार कॉल करना, कोड की एक निरंतर पंक्ति में। आइए एक उदाहरण देखें जहां मेथड चेनिंग हमें दोहराव से बचने में मदद कर सकती है।

उदाहरण

उदाहरण के लिए निम्न श्रेणी की कार लें -

class Car {
   constructor() {
      this.wheels = 4
      this.doors = 4
      this.topSpeed = 100
      this.feulCapacity = "400 Litres"
   }
   setWheels(w) {
      this.wheels = w
   }
   setDoors(d) {
      this.doors = d
   }
   setTopSpeed(t) {
      this.topSpeed = t
   }
   setFeulCapacity(fc) {
      this.feulCapacity = fc
   }
   displayCarProps() {
      console.log(`Your car has ${this.wheels} wheels,\
      ${this.doors} doors with a top speed of ${this.topSpeed}\
      and feul capacity of ${this.feulCapacity}`)
   }
}
let sportsCar = new Car();
sportsCar.setDoors(2)
sportsCar.setTopSpeed(250)
sportsCar.setFeulCapacity("600 Litres")
sportsCar.displayCarProps()

आउटपुट

Your car has 4 wheels,2 doors with a top speed of 250and feul capacity of 600 Litres 

देखें कि स्पोर्ट्सकार को कितनी बार अनावश्यक रूप से दोहराया जाता है? हम विधि श्रृखंला का उपयोग करके इससे छुटकारा पा सकते हैं। ऐसा करने के लिए, बसने वालों को केवल मूल्य निर्धारित करने देने के बजाय, इसे अंत में वापस कर दें। यह हमें ऑब्जेक्ट पर ऑपरेशन करने की अनुमति देगा। यह परिवर्तन करने के बाद, हमारा कोड इस तरह दिखता है -

class Car {
   constructor() {
      this.wheels = 4
      this.doors = 4
      this.topSpeed = 100
      this.feulCapacity = "400 Litres"
   }
   setWheels(w) {
      this.wheels = w;
      return this;
   }
   setDoors(d) {
      this.doors = d;
      return this;
   }
   setTopSpeed(t) {
      this.topSpeed = t;
      return this;
   }
   setFeulCapacity(fc) {
      this.feulCapacity = fc;
      return this;
   }
   displayCarProps() {
      console.log(`Your car has ${this.wheels} wheels,\
      ${this.doors} doors with a top speed of ${this.topSpeed}\
      and feul capacity of ${this.feulCapacity}`)
   }
}

अब हम उस हिस्से को बदल सकते हैं जहां हम अधिक पठनीय और कम दोहराव वाले कोड के साथ कार ऑब्जेक्ट बनाते हैं -

उदाहरण

let sportsCar = new Car()
   .setDoors(2)
   .setTopSpeed(250)
   .setFeulCapacity("600 Litres")
   .displayCarProps()

आउटपुट

Your car has 4 wheels,2 doors with a top speed of 250and feul capacity of 600 Litres 

मेथड चेनिंग को फ्लुएंट इंटरफ़ेस भी कहा जाता है क्योंकि यह ऑब्जेक्ट को बार-बार फ्लो को तोड़े बिना ऑब्जेक्ट को दोहराए बिना मेथड पर ऑपरेट करने की अनुमति देता है।


  1. जावास्क्रिप्ट गेटटाइम () विधि

    जावास्क्रिप्ट में गेटटाइम () विधि 1 जनवरी 1970 के बाद से बीत चुके मिलीसेकंड की संख्या लौटाती है। गेटटाइम () फ़ंक्शन के लिए कोड निम्नलिखित है - उदाहरण <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" conte

  1. जावास्क्रिप्ट सॉर्ट () विधि

    जावास्क्रिप्ट सॉर्ट () विधि का उपयोग किसी सरणी को सॉर्ट करने के लिए किया जाता है। छँटाई का क्रम वर्णानुक्रमिक, संख्यात्मक, आरोही या अवरोही हो सकता है। सॉर्ट () विधि के लिए कोड निम्नलिखित है - उदाहरण दस्तावेज़ बॉडी { फॉन्ट-फ़ैमिली:सेगो यूआई, ताहोमा, जिनेवा, वर्दाना, सेन्स-सेरिफ़; } .नमूना, .परिणाम {

  1. Object.fromEntries() जावास्क्रिप्ट में विधि।

    जावास्क्रिप्ट में Object.fromEntries () विधि का उपयोग एक सरणी की तरह एक पुनरावर्तनीय को परिवर्तित करने के लिए किया जाता है, मानचित्र में एक ऑब्जेक्ट में कुंजी मान जोड़ी होती है। Object.fromEntries() विधि के लिए कोड निम्नलिखित है - उदाहरण <!DOCTYPE html> <html lang="en"> <he