JS Function Methods call( ), apply( ), and bind( )

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 ต.ค. 2017
  • Functions all share three methods - call( ), apply( ), and bind( ).
    These methods let you call functions while letting you specify the context for calling the function. This lets you define what the "this" keyword will represent.
    call( ) and apply( ) let you pass in parameters to the functions when you call them too.
    bind( ) will let you set the context, pass in parameters, and return a copy of the function with the context and those values saved to be used later.
    Code GIST: gist.github.com/prof3ssorSt3v...

ความคิดเห็น • 95

  • @weronikadabrowska2829
    @weronikadabrowska2829 5 ปีที่แล้ว +22

    in that calming voice even javascript doesn't make blood boil

  • @jd.8019
    @jd.8019 4 ปีที่แล้ว +7

    I must say, you are a very good communicator. One of the most clear and concise explanations, in addition to your examples, I have seen. Thank you!

  • @pattyboi55
    @pattyboi55 5 ปีที่แล้ว +2

    Steve any time I get stuck on something I look you up to explain it. You are great!

  • @rotrose7531
    @rotrose7531 4 ปีที่แล้ว +3

    Every tutorial from you is GOLD, thank you.

  • @mrSargi7
    @mrSargi7 3 ปีที่แล้ว +1

    hurt my brain reading complicated articles with no results, thanks for this awesome explanation!

  • @captainalpha4853
    @captainalpha4853 5 ปีที่แล้ว +17

    Man, my life is useless without your courses! Thank you!

  • @qasimarthuna9254
    @qasimarthuna9254 4 ปีที่แล้ว +4

    I always look for your green thumbnail in my TH-cam search. And if I find I just don't even read your title and give it a shot.
    Because you are awesome and whatever knowledge you give, I want everything.
    Love you ❤️ from 🇮🇳

    • @6rs7rd7ts3
      @6rs7rd7ts3 3 ปีที่แล้ว

      XD, it funny because i do search the green thumbnail and with out thinking i click the video

  • @umarspi
    @umarspi 4 ปีที่แล้ว

    after many tutorials, i got a clear vision here. awesome approach brother. thanks a lot

  • @6rs7rd7ts3
    @6rs7rd7ts3 3 ปีที่แล้ว +1

    Always great content, easy to understand and a calming voice.

  • @shrikantg3860
    @shrikantg3860 2 ปีที่แล้ว

    Perfect. to understand call apply and bind thanks for sharing

  • @xeonace
    @xeonace 3 ปีที่แล้ว

    one of the best teachers about javascript on youtube

  • @ShreyasKumar
    @ShreyasKumar 3 ปีที่แล้ว +1

    Finally I binded myself to You.. Awesome teaching. Thank you.

  • @anokhps3088
    @anokhps3088 ปีที่แล้ว +1

    Its very clear,you are an amazing teacher is what all i have to say!Thank you.

  • @navidansari1895
    @navidansari1895 6 ปีที่แล้ว +1

    this is the best and very clear explaination to these fucntions till date...

  • @Dorff_Meister
    @Dorff_Meister 3 ปีที่แล้ว

    Fantastic explanations. I do believe my new knowledge will help me solve my current problem. Thanks!

  • @code-to-design
    @code-to-design 6 ปีที่แล้ว +1

    this one is my fifth, and i finally got it. amazing!

  • @mrcoder7327
    @mrcoder7327 5 ปีที่แล้ว

    This video really helped me improve my developing skills! Thanks

  • @huseynfy
    @huseynfy 2 ปีที่แล้ว +1

    Thanks man! Worth to watch.

  • @stunningmesh6263
    @stunningmesh6263 5 ปีที่แล้ว

    You are awesome please keep updating us with your knowledge

  • @SuperLukiano
    @SuperLukiano 5 ปีที่แล้ว

    Lost you on bind although I was already familiar with that function and I use it. You make it to complex for no reason. I was here for the call and apply and i got that one, thanks.

  • @jiteshnarula9257
    @jiteshnarula9257 4 ปีที่แล้ว

    Very nicely explained!
    Thank you very much :)

  • @kmchow4971
    @kmchow4971 2 ปีที่แล้ว

    I think you just saved me from the official document hell.

  • @jrs_devs
    @jrs_devs 2 ปีที่แล้ว

    Every time I need to properly learn some base functionality of javascript, there's one of your videos about the topic ❤❤

  • @NeilMyatt
    @NeilMyatt 5 ปีที่แล้ว +6

    7:20 cat falls off wardrobe, no-one notices :-)

  • @EconomicsDomain
    @EconomicsDomain 5 หลายเดือนก่อน

    Love the sound effects ;)

  • @persiansayed
    @persiansayed 4 ปีที่แล้ว

    Great, concise and simple explanation. I was reading lodash's implementation of "once" method. It calls "before" method in its body and there they have used "apply" to call the given function.
    Thank you, I was able to understand it after watching this video.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 ปีที่แล้ว +1

      You may also be interested in this - th-cam.com/video/J06Uz7m-Jn8/w-d-xo.html
      It is the vanilla JS implementation of once, in the browser.

    • @persiansayed
      @persiansayed 4 ปีที่แล้ว

      ​@@SteveGriffith-Prof3ssorSt3v3 Thanks, it's quite helpful. I really like the way you explain in-depth and core concepts in your videos, it's quite hard to find such type of videos nowadays.

  • @genadi2850
    @genadi2850 2 ปีที่แล้ว

    Very good explanation!

  • @chesterxp508
    @chesterxp508 2 ปีที่แล้ว

    Another very cool tutorial !!!

  • @MeshTheSnake
    @MeshTheSnake 3 ปีที่แล้ว

    amazing explanation, thank you!

  • @feroz1212011
    @feroz1212011 4 ปีที่แล้ว

    Wow...great explanation, thanks.

  • @user-zn2gn7pn1i
    @user-zn2gn7pn1i 5 ปีที่แล้ว +13

    7:19 I accidentally stepped on my cat's tail, I apologize :)))

  • @Thommyaso
    @Thommyaso 17 วันที่ผ่านมา

    awesome explanation, thanks

  • @walterokpala1229
    @walterokpala1229 ปีที่แล้ว

    Best explanation seen !!!!

  • @namlevan2929
    @namlevan2929 4 ปีที่แล้ว

    i 'm a little confused about these method. thank you so much
    i'm just subscribing your channel

  • @jalalbmnf
    @jalalbmnf ปีที่แล้ว

    Hi. Thank you, it is a great content

  • @nasarshaik5207
    @nasarshaik5207 3 ปีที่แล้ว

    sincerely thankyou so much sir..!

  • @twerner5496
    @twerner5496 3 ปีที่แล้ว

    It finally makes sense, thanks much

  • @piegpa
    @piegpa ปีที่แล้ว

    Excellent!

  • @ericalexander3816
    @ericalexander3816 4 ปีที่แล้ว +1

    This is the first time I've watched your video and know nothing about you. But I know that you are Canadian :).

  • @kurdactivitie4533
    @kurdactivitie4533 5 ปีที่แล้ว

    good job man, keep up.

  • @Jacobl119
    @Jacobl119 2 ปีที่แล้ว

    Thank you!

  • @RedEyedJedi
    @RedEyedJedi 5 ปีที่แล้ว

    Absolutely awesome tutorial, thank you Steve. Your videos are extremely helpful.

  • @donfully
    @donfully 5 ปีที่แล้ว

    Thank you sir

  • @Coxander
    @Coxander 5 ปีที่แล้ว +2

    Best explanation that I have found . Thank you a lot! One question, have you considered creating a full JS course with projects and exercises? I would gladly pay for something like that. Tnx

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  5 ปีที่แล้ว +7

      Thanks. It's what I do full-time as my job, teaching web development. I have given lots of thought to creating a course on one of the many platforms like Udemy. I have been approached by a number of schools and publishers to do this too. It's just hard to find the time on top of my full time work and keeping up this channel.
      Sharing my channel with more people does help me though, so please share it.

    • @Coxander
      @Coxander 5 ปีที่แล้ว +1

      @@SteveGriffith-Prof3ssorSt3v3 I have already recommended your YT channel on Reddit, and I will keep doing it in the future. I have learned a lot from you! I understand that you don't have enough time , but you are a great teacher and If you decide to create a course on Udemy I will support it 100%. Big thank you!

    • @mirzacerim6599
      @mirzacerim6599 4 ปีที่แล้ว

      @@SteveGriffith-Prof3ssorSt3v3 Just came here to say, you make a course on js, i buy it right away on udemy. Even if i would by that time get to a point where i dont need it, i would still buy it. Not just js, whatever web dev related you make.

  • @sol0matrix
    @sol0matrix 6 ปีที่แล้ว

    Excellent

  • @sergeymigel4680
    @sergeymigel4680 4 ปีที่แล้ว

    big thanks!

  • @ravithakor6238
    @ravithakor6238 5 ปีที่แล้ว

    easiest explanation nice

  • @Snoo29293
    @Snoo29293 3 ปีที่แล้ว +2

    I didn't really understand why we needed to use call for the method example, doing this would work too:
    function bob(name, age){...}
    let obj = {
    method(fn, n, a){
    fn(n, a);
    }
    }
    obj.method(bob, "Jack", 16)

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 ปีที่แล้ว +3

      most of the time it is used for delegation. The first parameter in call( ) apply( ) or bind( ) is what you want to use as `this`. You get to change the context for calling your method and do it with a single line of code.
      There are always alternative ways of doing pretty much anything in JavaScript. It is about understanding how all the tools work so you can use the best one for your current situation.

  • @codea8901
    @codea8901 2 ปีที่แล้ว

    Amazing tutorial once again Steve! One question I have is that in the bob function you have a return true statement at the end and wondered what's the purpose of that please? Many thanks.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  2 ปีที่แล้ว +1

      Just to illustrate that when you use bind you get back a copy of the function, not the true.

    • @codea8901
      @codea8901 2 ปีที่แล้ว +1

      @@SteveGriffith-Prof3ssorSt3v3 Thank you.

  • @jasbindarsingh1640
    @jasbindarsingh1640 4 ปีที่แล้ว

    hey Steve, great video understood the working of call, apply, and bind, but can you state where I could use it and why, why do we pass a context to a function when it is doing the same thing.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 ปีที่แล้ว +2

      When you need to create a closure around a value that will be used in the function when it gets called or when you need to change the context in which the function will run.
      Not something that you need to worry about until those other concepts make sense and you realize that you need them.

    • @jasbindarsingh1640
      @jasbindarsingh1640 4 ปีที่แล้ว

      @@SteveGriffith-Prof3ssorSt3v3 thanks Steve, btw I am seeing a lot of things in JS revolve around closures.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 ปีที่แล้ว

      @@jasbindarsingh1640 Closures are definitely a core feature in JS in how scope is handled.

    • @genovo
      @genovo ปีที่แล้ว

      Apply is Array, Call is Comma, Bind is 'BloBject'. Dumb. I know, like a Blontext.

  • @noonecares6816
    @noonecares6816 11 หลายเดือนก่อน

    Sorry where did we get fn() function inside the method?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  11 หลายเดือนก่อน +1

      When we call bill.myMethod( bob ) we are passing in bob, which is a function.
      So, inside the bill object...
      bill = {
      ...
      myMethod: function (fn) {
      //fn is the function bob that we passed in
      }
      }

  • @ahmadmohamad8416
    @ahmadmohamad8416 2 ปีที่แล้ว

    thank you

  • @gtsin
    @gtsin 3 ปีที่แล้ว +1

    This is so complicated. Is there a better way than using call, apply, bind methods in 2021?

  • @mrlectus
    @mrlectus 9 หลายเดือนก่อน

    Wish there was a real world practical example

  • @shubhsagar6234
    @shubhsagar6234 3 ปีที่แล้ว

    Sir, I expected when Bob was called inside the bill object then the value of 'this' should be object bill not the window object...

    • @shubhsagar6234
      @shubhsagar6234 3 ปีที่แล้ว

      I got it after the full video... Thanks... Yo

  • @RedEyedJedi
    @RedEyedJedi 5 ปีที่แล้ว

    One question though Steve. If, fn.apply(bill, [n, s]); does exactly the same thing as fn.call(bill, n, s); why would you choose the version with 3 extra characters? Are you some sort of secret keyboard torturer? haha

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  5 ปีที่แล้ว

      Sometimes you will have a single variable which is an array. Other times you will have several variables. Choose the version that works for your circumstances.

    • @RedEyedJedi
      @RedEyedJedi 5 ปีที่แล้ว +2

      @@SteveGriffith-Prof3ssorSt3v3 So the only difference between call and apply, is the parameters it takes?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  5 ปีที่แล้ว

      @@RedEyedJedi yep

    • @RedEyedJedi
      @RedEyedJedi 5 ปีที่แล้ว +1

      @@SteveGriffith-Prof3ssorSt3v3 Thanks Steve, I appreciate you taking your time to respond.

  • @yoelmarquez5029
    @yoelmarquez5029 3 ปีที่แล้ว

    Still a bit confusing but this helped

  • @orangelimesky
    @orangelimesky 24 วันที่ผ่านมา

    David Attenborough of JavaScript YT Videos

  • @aba3722
    @aba3722 3 ปีที่แล้ว

    영어 모르는 한국인을 울고갑니다

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 ปีที่แล้ว

      :) And I wish my Korean was better so I didn't have to use Google Translate to read your comment.

  • @shrestha0144
    @shrestha0144 2 ปีที่แล้ว

    7:20 was that a cat ?

  • @kulbhushandhongade382
    @kulbhushandhongade382 4 ปีที่แล้ว

    where you are going man its not good

  • @hamidRaven13
    @hamidRaven13 ปีที่แล้ว

    this keyword this was quite confusing

  • @SanjarKodirov-ww3le
    @SanjarKodirov-ww3le 2 ปีที่แล้ว

    Hi i really love your lessons. But thins mouse voice is soo annoying can you fix this please ))))

  • @williamjamesrapp7356
    @williamjamesrapp7356 4 ปีที่แล้ว

    Sorry but it seems that all people who are smart at coding know little to nothing about TEACHING.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 ปีที่แล้ว

      Are you referring to me? Or am I not understanding what you are trying to say?
      Because I have been an award winning professor for nearly 20 years. There is also a difference between explaining a method or concept and a tutorial that explains the exact solution to a problem that someone is having.

    • @williamjamesrapp7356
      @williamjamesrapp7356 4 ปีที่แล้ว

      @@SteveGriffith-Prof3ssorSt3v3 I apologize. My own frustrations and inadequacies in learning I have spoken out of turn. Obviously your courses or examples here are not intended for people trying to learn JS but for people who already know JS and you are, giving examples of what ever this is. I am looking for something more along the beginners side of teaching CALL's are, you know someone who is trying to learn how to implement this in a real world situation and without context, like your example, to me this might as well be Chinese spoke by a Greek Professor to a class room full of Mexicans. It means nothing to me and there in lies my frustrations and I apologize if my comment , as smart and as award winning as you may be, has offended you.
      I have become comfortable with HTML, CSS, PHP, SQL, but as many hours of JS as I have watched it is abstract information to me without anyone ever using it in context to a WEB SITE or how to actually implement this in a web site. THAT IS MY ISSUE and NOT YOURS and I again apologize for offending you. But if this is how you TEACH I would not want to take one of your classes.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 ปีที่แล้ว +2

      No offence taken. JavaScript is a huge topic. There is some that applies directly to the browser and some other things that have nothing to do with the browser. This topic is one that I teach my students after about 3 - 4 months worth of prior JS training. It's not a topic that is necessarily directly applicable to a web site.
      I have a playlist called JS from the Start. This list is about the topics that are just JS programming topics, not things that need a browser. I have another big playlist that is called JS in the Browser. That list is about scripting things for a website.
      I have lots of other smaller playlists about specific topics like loops, functions, arrays, AJAX, HTML5 APIs, etc. those lists have a mixture of things that are browser dependent and things that have nothing to do with websites, they are just programming concepts.
      The programming concepts are about learning the tools of the language. Once you know enough tools then you can start to use combinations of those tools to solve problems.
      If you are looking for website specific stuff, since you already have some programming experience with PHP, I would recommend that you look through the JS in the Browser and the HTML5 APIs playlists. Those sound more like what you are looking for.

    • @williamjamesrapp7356
      @williamjamesrapp7356 4 ปีที่แล้ว +1

      @@SteveGriffith-Prof3ssorSt3v3 Thank you for your time and response. Have a fantastic weekend.

  • @yaolegoleynik
    @yaolegoleynik 4 ปีที่แล้ว

    Thank you!