javaScript Method chaining tutorial ( function chaining)

Поділитися
Вставка
  • Опубліковано 30 вер 2024
  • We will learn how to jquery style chain functions ( Methods) using pure javaScript. in Jquery we can do $('.class').addClass('new').removeClass('old'). we can achive similar chaining using pure javascript.
    Method chaining, also known as named parameter idiom, is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results.
    javascript advance tutorials
    javascript function chaining
    jquery like function chaining
    javascript how to chain functions
    java script function chaining
    method chaining
    chaining module method calls
    method cascading
    ECMAScript 5
    ECMAScript6
    #JavaScript #FunctionChaining #MethodChaining
    *My Udemy Courses
    www.udemy.com/...
    *couponCode*=TECHSITH-9.99
    www.udemy.com/...
    Follow me for technology updates
    * / techsith
    * / techsith
    * / techsith1
    * / 13677140
    * / patelhemil
    Help me translate this video.
    * www.youtube.co...
    Note: use translate.goog... to translate this video to your language. Let me know once you do that so i can give you credit. Thank you in advance.

КОМЕНТАРІ • 83

  • @MyGeorge1964
    @MyGeorge1964 7 років тому +14

    Note the shorthand instead of "{add: add, substract: substract, print: print};" you can now write
    return {add, substract, print}; And thanks for this clear demonstration.

    • @Techsithtube
      @Techsithtube  7 років тому +1

      yes. thanks for pointing it out.

  • @keronwilliams5392
    @keronwilliams5392 3 роки тому +1

    Is it possible to chain methods from factory functions? E.g. sayName().location().favFoods(). I can't use the `return this` keyword inside the factory function like how you would with a regular constructor function :(
    const nerd = (user, city, food) => {
    const { sayName, location } = Person(user, city)
    const { favFoods } = FaveFood(food, user) // console.log(`${user} likes nerd stuff!`)
    return { sayName, location, favFoods, doSOmethingNerdy }
    return this
    }

  • @siu17
    @siu17 5 років тому +8

    Great tutorial, but “substract”??

  • @krishnayadav2879
    @krishnayadav2879 3 роки тому +1

    Anyone Here to know how Promise.then().catch() chained together ?

    • @Techsithtube
      @Techsithtube  3 роки тому

      Promise returns an object with "then" method, then method returns an object with "catch" method.. :)

  • @shyamjaiswal5402
    @shyamjaiswal5402 5 років тому +1

    x.add(3) is fine when we call it, why undefined ? inside the console.log?

  • @angshu7589
    @angshu7589 5 років тому +1

    The only video I understood.

  • @karthikpanjaje7853
    @karthikpanjaje7853 6 років тому +3

    Subtract*

  • @creative-commons-videos
    @creative-commons-videos 2 роки тому

    hi, how to limit method chain to some perticular function ? returning this will give all method access, and i am trying to make a routing package where i need to return a chain of method like router.get() here i only want to return get, post and other method but i have many methods in class that all are showing up, i also have applyMiddleware() method that i only want after method is selected but it is showing while accessing router.* anything

  • @carloseduardogomezlozano491
    @carloseduardogomezlozano491 4 роки тому

    one question, please is it correct to do the following code?
    var obj = function () {
    var oper = {
    i: 0,
    add: function (j) {
    this.i += j
    return this
    },
    sub: function (j) {
    this.i -= j
    return this
    },
    print: function () {
    console.log(this.i)
    },
    }
    return oper
    }
    var x = obj() //no need new
    x.add(3).sub(2).print() //1
    console.log(x)
    I hope your answer, thank you. Blessings

  • @injrav
    @injrav 8 років тому +1

    very very good example .
    Not only method chaining
    the example at the end is very useful for writing a class with private fields and public methods..

  • @vigoworkchannel1681
    @vigoworkchannel1681 3 роки тому

    This man is the best teacher, UA-cam has been recommending the wrong people to me

  • @sanjayvns1987
    @sanjayvns1987 2 роки тому

    What is difference in defining a variable like $url or var url in js

  • @TubeFreak44
    @TubeFreak44 3 роки тому

    Calling both variables "i" is kinda misleading.

  • @yasirkaram
    @yasirkaram 5 років тому

    I have never found most precise, concise and trick catching instructive series on UA-cam or Udemy like this, really good baked pastry been brought only by "bakers". If you would author a book on JS it will be of most useful one

  • @sunilkumar-tk3gu
    @sunilkumar-tk3gu 6 років тому

    Return this is instant object, what is value hold by this element, it is difficult to understand

    • @Techsithtube
      @Techsithtube  6 років тому

      'this' means the reference to the entire object.

  • @SheshagiriPai
    @SheshagiriPai 8 років тому +6

    I have always added new when creating the object. How did you get away with removing the new at 10:08 Can you please elaborate. Thanks :)

    • @Techsithtube
      @Techsithtube  8 років тому +21

      +Sheshagiri Pai Thanks for watching the video . Following is the explanation you asked.
      var obj = function() {
      this.i = 1;
      this.geti = function(){ return this.i;};
      };
      the above behaves more like a class. so if i need to create an object from this class i can use 'new' like this
      var a = new obj();
      var b = new obj();
      var c = new obj();
      as you can see i can create multiple object from this obj class function . Basically 'new' here means creating a new object.
      Now lets look at closure
      var obj = function() {
      var i = 1;
      var geti = function(){ return i;);
      return {geti:geti};
      };
      var a = obj();
      here i when i do var a= obj(); , i am not creating a new object . obj() is actually returning me a object with function geti . you can see in this statement "return {geti:geti};" that is why i am not using new.
      also in the first example you dont see return statement.
      Thanks

    • @meriemkaroun7626
      @meriemkaroun7626 8 років тому

      And it is always a function, isn't?!
      var obj = function(){
      this.num = 0;
      };

  • @mysticfakir2029
    @mysticfakir2029 3 роки тому

    Why not just write a class instead of an object that acts as a class? Why in good heavens does Javascript even give you the option to do this?

    • @Techsithtube
      @Techsithtube  3 роки тому

      Classes are there in JS but not used as much. Frameworks like react are completely moving away from classes.

  • @karthiknedunchezhiyan7935
    @karthiknedunchezhiyan7935 7 років тому +2

    jillakidumma

  • @muhammadsafiullah8428
    @muhammadsafiullah8428 5 років тому +1

    thanks for this tutorial.

  • @sudiptasarkar4438
    @sudiptasarkar4438 5 років тому

    When you write this.i += i inside this.add func() why it isn't creating a new this? I thought only arrow func doesn't have their own this

  • @ECUdweber
    @ECUdweber 8 років тому

    Great video. FYI, it's "subtract".

    • @Techsithtube
      @Techsithtube  8 років тому +1

      +ECUdweber Thanks for the correction you are the first one to notice. :)

  • @compromisenonono8762
    @compromisenonono8762 8 років тому

    it seems that the returned object is nothing but bunch of functions,these bunch of functions share the out parameter i,they can modify this i and return the object itself,which was cool, thanks sith!

  • @rapoliit
    @rapoliit 2 роки тому

    Simply superb ☺️

  • @rahulmarne4112
    @rahulmarne4112 7 років тому

    javaScript Method chaining tutorial ( function chaining) what you want to explain in this video did get it please tell me

  • @faisalnaseer798
    @faisalnaseer798 7 років тому +2

    The best one

    • @Techsithtube
      @Techsithtube  7 років тому +1

      I am glad it helped. Thanks for watching!

  • @sudiptasarkar4438
    @sudiptasarkar4438 5 років тому

    What is the logic behind return this; after you made 'i' as private variable?

  • @rembrandt702
    @rembrandt702 8 років тому

    You don't create a closure, you use a closure because closure is a feature of the language.

  • @williamheckman4597
    @williamheckman4597 5 років тому

    I just wish the keyboard clicks could be louder...

  • @devope
    @devope 6 років тому

    so, all this 2 examples are working, so I can use any of them?

  • @ritsk4338
    @ritsk4338 5 років тому

    Method chaining on closure is bit tough to understand for me

  • @anupsingh24
    @anupsingh24 6 років тому

    Kamaal karte ho Patel ji

  • @lycan2494
    @lycan2494 5 років тому

    so function xpressions inside a function expression

  • @bagoquarks
    @bagoquarks 8 років тому +3

    Nicely done. A very good use of 12 minutes for beginning JS programmers.

  • @hn031
    @hn031 3 роки тому

    This is a topic that's been on my mind for some time now, I've read blogs about method chaining but never quite understand it. Watching this video now makes me understand it better. Thanks

    • @Techsithtube
      @Techsithtube  3 роки тому

      Glad it was helpful! Thanks for watching!

  • @samyever2be
    @samyever2be 7 років тому

    Hi, I have tried in my localhost, but i didn't get any console output, please guide me

  • @stevkoria
    @stevkoria 7 років тому +10

    are bhai substract nahi, Subtract :-)

  • @moolipit
    @moolipit 8 років тому

    i dont understand how come you were able to get the correct result here 04:45 but when you chain them you had to return the object first ???

  • @vineetharkut4920
    @vineetharkut4920 6 років тому

    Nice explanation sir.. I have one doubt is that what is difference between creating object with new and without new operator because which i know (if i am wrong then rectify me) that while creating an object with new operator they maintain there own data member & method then i tied with above example without using new operator i was thinking that may be they are referring to same address/reference
    Without Using new Operator
    var x = obj();
    var y = obj();
    console.log(x===y) // false
    x.add(3).substract(2).substract(2).print(); // -1
    y.add(3).substract(2).print(); // 1
    With Using new Operator
    var x = new obj();
    var y = new obj();
    console.log(x===y) // false
    x.add(3).substract(2).substract(2).print(); // -1
    y.add(3).substract(2).print(); // 1
    can you help me to clear my this doubt..Thanks

    • @Techsithtube
      @Techsithtube  6 років тому

      What you are looking for is understanding of how to create objects diffrent ways. Here is one of my tutorial that explains that. ua-cam.com/video/xizFJHKHdHw/v-deo.html

  • @Owenimus
    @Owenimus 5 років тому

    How can I do this asyncronously?

    • @Techsithtube
      @Techsithtube  5 років тому

      You have to return a promise for it to execute asynchronously.

    • @Owenimus
      @Owenimus 5 років тому

      techsith That would return the promise itself, not the class, therefore I wouldn't be able to chain it.

  • @KrishnaKamal49
    @KrishnaKamal49 8 років тому

    This is very helpful, thanks for sharing your knowledge!

  • @sandipdas-cm5yz
    @sandipdas-cm5yz 7 років тому +1

    very nice

  • @SirusDas
    @SirusDas 7 років тому

    Hi, I liked your tutorial. Is there other way than just return{add:add, substract:st...} and why didn't you use new obj()??? Could you please guide me :)

    • @Techsithtube
      @Techsithtube  7 років тому

      when you run the function it's returning the object with mehtods with closures. which means this is not a function constructor. only function constructor uses new. I have a tutorial on that called "constructor" you can look at that. You can return like this
      jsfiddle.net/7dg1g3j8/ does that answer your question?

    • @SirusDas
      @SirusDas 7 років тому

      Your quick response is highly appreciated. I really liked your example at jsfiddle.net/7dg1g3j8/ and something like this "const cal = () => {" is very new to me. It was a great learning. Thank you for sharing your knowledge. Have a great time ahead and keep publishing :)

  • @mdshoaibAlamgcetts
    @mdshoaibAlamgcetts 6 років тому

    when we should use 'new' keyword and should not , please help me to understand this

    • @Techsithtube
      @Techsithtube  6 років тому +1

      new is a constructor pattern. Basically when you want to create objects from a constructor. If you are doing functional programming where you are returning functions with closures and such you are using with out new. You should watch my object oriented series to understand 'new' keyward . ua-cam.com/play/PL7pEw9n3GkoW0ceMeoycg9D00YjPAbtvt.html

    • @mdshoaibAlamgcetts
      @mdshoaibAlamgcetts 6 років тому

      techsith thank you so much ....I have learned alot from your videos . Once again thanks for sharing playlist will go through all

  • @webpocetnik7862
    @webpocetnik7862 7 років тому +2

    You are very good at explaining things.

  • @jayaprakash456abc
    @jayaprakash456abc 8 років тому +1

    Very helpful. I am newbie to java script. Thanks for the video.

  • @sayhongtan8993
    @sayhongtan8993 8 років тому +1

    This is great! Illustrates Method Chaining clearly!

  • @doit4941
    @doit4941 7 років тому

    why return "this" ?

    • @Techsithtube
      @Techsithtube  7 років тому +1

      When you return this. you return refrence to yourself ( in this case the main function ) , this way now you can can call the methods defined in itself using dot notation . x.add().add();

    • @guimarquesini
      @guimarquesini 7 років тому

      but in the 'add' method for example, returning 'this' there will return the main function itself, but I don't understand why it doesn't return the add function

    • @Techsithtube
      @Techsithtube  7 років тому

      In this case you only have one function. what if you have many functions like "add" you dont want to return only one function . instead you should return "this" so you can chain any functions.

  • @VishnuPrasadmsvp
    @VishnuPrasadmsvp 7 років тому +1

    Very nice explanation.. hurray👏👏

  • @coolprashantmailbox
    @coolprashantmailbox 8 років тому +1

    awesome sir..your advanced js tutorial

  • @kemmrthappy2804
    @kemmrthappy2804 7 років тому +1

    i must very interesting tutorials , you simply made it so easy to understand ,thanks a lot

  • @nafeesahmed4942
    @nafeesahmed4942 7 років тому +1

    very nice explanation.

  • @reallybigbiscuit3568
    @reallybigbiscuit3568 8 років тому

    This was extremely useful. Thank you for the straighforward and great tutorial.
    :D

  • @tanisha4568
    @tanisha4568 8 років тому

    Hi, Thank you for the wonderful video. Can you please share your jsFiddle link here.
    Thanks,

  • @MostwantedKamal
    @MostwantedKamal 8 років тому

    thanks for a great tutorial..it completely makes sense for me ..

  • @deividas777
    @deividas777 7 років тому

    Thanks great explanation, easy to follow

  • @bboyjojo1988
    @bboyjojo1988 6 років тому

    Nice lesson, thank you for teaching, keep up the good work, peace!

  • @tzetrikoiand441
    @tzetrikoiand441 8 років тому

    Very informative video!

  • @MrChubib0
    @MrChubib0 7 років тому

    this guy is on point! great teacher

  • @mohammadadilalam7185
    @mohammadadilalam7185 7 років тому

    really helpful, nice one