ไม่สามารถเล่นวิดีโอนี้
ขออภัยในความไม่สะดวก

JavaScript for Developers 36 - Functions as arguments

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ก.พ. 2016
  • Access the full course here: javabrains.io/...
    Thanks to functions being first class values, they can be passed as arguments to other functions. Let's see how that works!

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

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

    Hey Koushik, wonderful explanation. Thanks for keeping it simple as usual.

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

    Aha ! A big moment of understanding! Thanks for making some long-term confusion instantly get resolved.

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

    Thank you for Clearing the Concept

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

    Thank you! I was watching another tutorial and he did this, but didn't explain it, and I was left scratching my head. After a bit of Googling, I found this video and it helped me immensely! Thank you very much!! Hoping to see some Vue stuff from you in the near future! :)

  • @bhaskarbhardwaj9448
    @bhaskarbhardwaj9448 8 ปีที่แล้ว +3

    Superbly Explained !!

  • @didondio
    @didondio 7 ปีที่แล้ว +13

    Wow, Javascript is weird !

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

    Very helpful, thank you!

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

    Thank you very much!

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

    Hi, I just want to ask to ask about return statement, in the very first function if I provide a return statement then how can I access the return statement with the second function or pass a parameter for return statement.

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

    thank you!!!

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

    There is no semicolon after the executor function.
    It is defined to be as a value to a variable in the video.
    Is it correct or JS interpreter just ignores it but it should be there?

    • @Java.Brains
      @Java.Brains  8 ปีที่แล้ว +7

      +Rhitik Bhatt (Lambu) You are right. It's good practice to add a semicolon at the end of that expression there, and I forgot to add it. But it's not mandatory though. The JavaScript interpreter figures out that it's the end of the line anyway, which is why it works.

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

    hello there , can you show this with currying ?
    I am struck in a middleware in redux project code where we are currying some action I am not getting an argument is being called as function/method.
    export const loggerMiddleware = (store) => (next) => (action) => {
    if (!action.type) {
    return next(action);
    }
    console.log('type: ', action.type);
    console.log('payload: ', action.payload);
    console.log('currentState: ', store.getState());
    next(action);//I am not getting this line
    console.log('next state: ', store.getState());
    };

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

    var f =function (name){
    console.log("Hello "+name);
    };
    var executor = function(fn,name){
    fn(name);
    }
    executor(f,"vinay");
    From bottom line 3, why we have to give the argument name in that?

  • @bitoutsid-ahmed7852
    @bitoutsid-ahmed7852 4 ปีที่แล้ว

    hello, and thanks, just question, what if we have many argument like name ,sruname and so on ,and instead of console log return ?

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

    hi Kaushik, can you please explain me the output of this code
    var f= function (){ console.log ("hello")};
    var check = function(f)
    { console.log ("just checking " + f())};
    check(f);
    output is : hello
    just checking undefined
    Why there is undefined printed after "just checking"
    thanks in anticitpation

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

    I understand callbacks in C++ where you pass a 'pointer to a function' to another thread and wait for the callback result. This does not appear to be the same as a C++ callback. Maybe I'm not understanding the usecase in javascript, but this seems pointless. What problem does this technique solve?

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

    Why do functions get invoked as CALLBACK functions when stored in a variable? And yet when a normal function is stored in a variable it is not invoked. Where is the critical difference?

  • @burakyigit1446
    @burakyigit1446 7 ปีที่แล้ว +2

    what is the advantage of this usage? Where can this be used fairly?

    • @FiruzzZ-777
      @FiruzzZ-777 6 ปีที่แล้ว +1

      You can encapsulate behaviors and send them as a parameter or return them. In another language you would do this with nested if-else

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

    How does line 7 or fn know to call line 1 (var f)?

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

    Would be great if you could have added the word "Call back" function concept in this.

  • @indraalapati989
    @indraalapati989 8 ปีที่แล้ว

    Hi Koushik, i am not able to run javascript code from scratchpad. Should i download any plugins ? Please help me out. Thanks in advance.

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

      Scratchpad is a part of Mozilla Firefox

  • @athrims4007
    @athrims4007 7 ปีที่แล้ว

    I was just trying some sample program to follow the tutorial and I came across the following behaviour.
    var func = function(){
    return 20;
    };
    function print(fn){
    console.log(fn());
    }
    print(func);
    When I use the function name as print , nothing gets printed to console, but print is not even a reserved word.
    Can you please tell me why it behaves like this.

    • @finkygsd2720
      @finkygsd2720 7 ปีที่แล้ว

      try using "use strict"; and see if print works.

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

    var f=function (){}; and function f(){}; seems to be the same thing.

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

      you cant use just "f" as a function on second expression.. you have to put "f()" like this..

  • @wvvwwwvvw
    @wvvwwwvvw 8 ปีที่แล้ว +3

    So what is the point of giving a _name_ in the function expression, if you cannot call it later on in the code?
    Here is the example code with comments:
    jsfiddle.net/oleshko/5jdt769b

    • @Java.Brains
      @Java.Brains  8 ปีที่แล้ว +6

      +Aleksandr Oleshko Honestly, I have *always* used anonymous function expressions, because it doesn't make sense to have two names for the same thing anyway. Good to know that it doesn't even work! :)

    • @jakedotnet
      @jakedotnet 7 ปีที่แล้ว +6

      You can use the name inside of the function you create with the function expression, but not outside. That way, you can call the function recursively regardless of the variable name it has on the outside. Another benefit is that some browsers / JS runtime environments will show the name in debugging, which may be helpful.
      var fn = function factorial(n){
      if (n < 0) return NaN;
      if (n == 0) return 1;
      return n * factorial(n-1);
      };
      var g = fn;
      var res_fn = fn(3); // doesn't depend on the name fn or g, still works
      var res_g = g(3);
      factorial(3); // doesn't work since the name isn't available outside

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

    hii! nice video! very informative!! can u please solve this doubt of mine.... in a function I have seen people put like function trial(obj){}. here, obj has some definite meaning or is like just some word.. pls reply asap

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

    Over simplified. In real world scenarios it's much more complicated.

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

    got it

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

    thank you so `${much}`

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

    i see indian, i click

  • @ericprak
    @ericprak 7 ปีที่แล้ว +2

    I still don't get point of all this. WHERE or WHEN would this be applicable in a real life programming job? Instead of having a second function execute the first function, why not just simply execute the first function itself?
    This just seems like a pretentious convoluted way of executing an initial function, I really don't see how this makes "asynchronous programming" possible.

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

      ReactJS does pass functions around like this. However, I have never done this with vanilla javascript.
      Somebody asked me this during an interview for "JavaScript Developer" 3 weeks ago. Chance are that is one of the only times somebody will ask you to write this puzzle.

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

      do you remember when the terminator just loaded a program to fly a plane, then just like that he could fly a plane? yea... something like that

  • @Chris-pv8oj
    @Chris-pv8oj 6 ปีที่แล้ว

    Why you use dip... Smokeless tobacco is just as dangerous

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

    Please speak clearly. Thank you