Javascript is Weird…and AWESOME
LearnCode.academy · 5 lecciones · Gratis en YouTube
Javascript Context Tutorial - What makes Javascript Weird...and Awesome Pt5
Sobre este curso
all right so I'm going to be doing five videos for you here and they're going to be the five things that make JavaScript super weird but also that make
JavaScript awesome these are five of the most misunderstood or barely understood or not even understood things about JavaScript they're going to be very useful very very useful if you're new to
JavaScript and honestly lots of intermediate developers still don't grasp these things very well so we're going to be covering them I'm going to do a video on each so you can re-watch
it they build on each other for the most part they all build on each other and so you'll want to rewatch it to you kind of get it and then move on with each one so let's get into the first one one of the
first things that makes JavaScript weird is first class functions what this means is that functions can be passed as arguments and if you don't understand it you're not going to understand why a lot
of the Java scrap JavaScript syntax is the way it is let me show it to you in code it's a lot more easy you know a lot easier to understand let's go make a function called add first second and all
this is going to do is consult log first plus second all right there we go and let's do something let's go add two plus three let's add four plus five let's hit save and there we go I don't know if
that's big enough for you but we we console.log 2 plus 3 and Wii console logged 4 plus 5 now let's add a third one called callback and this is dumb you see a ton and jQuery whatever the
optional last argument is a callback function to fire when it's done and so let's create a function here and this one we will console.log done so there we go and now I need to do something with
callback here so I'll go callback and I'm going to actually fire it when I'm done with my addition here so what I've done is I've given an argument 1 & 2 our numbers argument 3 is a function I'm
creating right now and I'm passing that function up here it's not firing yet it's being passed as an argument that ad is going to hold on to it's going to do everything it needs to do
in this case is just addition and then it's going to fire that function later as a callback let me show you what this going to so now it's going to say it's going to console.log two plus three and
then it's going to console.log done so let me actually create another one here and this is going to console.log done again so this is going to do the exact same thing it doesn't care what your
callback function is it's just going to fire it when you're done so it's going to console.log two plus three done console.log four plus five and then it's going to fire my other function again
and so that is what it means to pass a function a function is a first-class object and so here's kind of another way to look at it is a function is basically a variable I can go function log done
and that will console.log done there we go and so now I can actually pass log done and let's go ahead and mute that guy so there you go exact same thing I'm
creating a function here called log done I'm passing it as if it was a variable because to JavaScript it is a variable and so I'm passing log done in it's going to get it's now going to be
associated as callback and it will get fired whenever and honestly in jQuery what you're actually seeing is something more like this if callback then fire callback so that way you don't have to
pass callback I can just do console log four five here there you go so now it's going to do two plus three fire the callback because it exists fire four plus five and don't fire the callback
because it doesn't exist and so that's kind of little bit closer to what's going on it's more complicated than that but if you want to build your own callbacks that's a nice way to make them
optional and so that's what it means for them to be first-class functions so in jQuery when you're doing stuff like my div click function you're actually creative function right
there that you're passing to the click and it's not going to do anything with it until the click event fires and that's why you can also go function handle click you know do something smart
there something smart and then you can actually go handle click and that looks a lot cleaner there that's a lot of cleaner way to do a lot of your jQuery and that kind of leads us into video
number 2 which is event-driven environment let's get into that
SIEMPRE HAY ALGO MÁS