if folks is prying right back with another RWD screencast today we're going
to be looking at callback functions we're going to talk a little bit about
how you can implement your own callback functions this is something that has
always eluded me a bit and I just thought that it would be helpful to kind
of explain it talk it through both for my own benefit and for your benefit to
hopefully get a little bit of a better understanding about how these work so
what we're going to do is we're going to take a look at a example here we're
going to create we're going to create a boilerplate HTML file I'm going to add
in a script here and within this script what we're going to do is just to start
out we're going to create a function this is going to be our call or function
this eventually will be the function that we'll use to execute our callback
and we're just going to use this as a way of logging a statement back to the
console this is going to tell us that this is the caller function we're going
to execute that function if you look here when we execute that caller
function we can see that it is pushing the value from the console that logged
back to the back to the console so this is exactly what we'd be expecting and
it's a fairly trivial example but what we want to do is we want to look at how
we can actually pass a function to within this caller function and execute
that after the rest of this function has completed so we are going to use a
feature called the call function which is part of the function prototype so
it's going to allow us to execute the function that is passed to
caller and we're going to pass an anonymous function that function is just
going to also do a console log and it's going to tell us that this is the
callback function the expected behavior here would be that our function will
console.log the caller function and it'll subsequently console.log out the
callback function which is exactly what we're seeing here now in addition to
that what we want to do is we want to make this a little bit more robust so
instead of using the call function we're going to use the bind function the bind
function is going to make sure that we we retain our scope and it's going to
allow us to be able to bind this callback function to a function within
our caller called CB and then instead of passing an anonymous function in there
we are going to create a new function we're going to call that fun and this is
just going to be a just but for readability so instead of passing an
anonymous function to caller we're just going to pass fun and this should behave
exactly the same way as the last example except we are now binding the function
just going to help us a little bit when we start passing parameters so we are
going to in addition to passing fund we are also going to pass a string value
into our caller function and this string value is going to be the parameter that
we're going to pass to fun and this will actually pass allow us to within the
bound function to be able to pass the parameter value to the function that
we've defined and just for scoping purposes I'm going
to create a parameter I'm going to name it P and then we're going to append P to
our string and we are going to finally in our CB function we're going to make
sure that we pass the parameter to our caller function make sure that it's in
our function definition we are again going to just assign P to a local
parameter value to ensure scoping and we're going to pass P to that function
and now we can see that it is calling that function it is executing all of the
logic within the caller function it is binding the unction to our to our CB
local function and it is passing P to our function and allowing it to accept
the parameter that's being passed and the benefit here is that we now have the
ability to to execute a function and then define what is going to get
executed after that function completes hopefully this gives you a little bit
more clarification on how binding and how callback functions in general work
within JavaScript and always welcome comments so if you've got some comments
or questions please leave them in the comments section or get me on twitter at
ignore intuition thank you




For more infomation >> Tawa pizza or pizza on pan recipe - How to make veg pizza on pan - Duration: 8:34. 






For more infomation >> How to use dual sim with SD card in hybrid sim slot - Duration: 3:42. 




Không có nhận xét nào:
Đăng nhận xét