Got your callback

Developer
Size
2,451 Kb
Views
30,360

How do I make an got your callback?

What is a got your callback? How do you make a got your callback? This script and codes were developed by Nick Williams on 27 August 2022, Saturday.

Got your callback Previews

Got your callback - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>got your callback</title>
</head>
<body> <script src="js/index.js"></script>
</body>
</html>

Got your callback - Script Codes JS Codes

var someArray = [5,6,7,8,9];
// first let's write a really naive function
// that loops through the above array and
// prints them numbers out
function each(collection) { var item; for(item in collection) { if(collection.hasOwnProperty(item)) { // assuming callback was a function, // call it and pass in the item console.log(collection[item]); } }
}
each();
// this does the job, but it's a bit crap. It's
// really inflexible, because it can only log
// to console, what if we wanted to do other things?
// We want to be able to modify functionality without
// changing the core code itself.
// Enter callbacks!
// Modern JS engines have a forEach method on
// the array prototype, so you can do stuff like
// [1,2,3,4,5].forEach(callback)
// Let's use it and pass in a callback to
// log the numbers to the console, replicating the
// functionality above.
someArray.forEach(function(item) { console.log(item);
});
// This of course has accepted a callback.
// So mimicing this functionality is a good
// lesson in writing code that has callbacks.
// Let's do that now...
function each(collection, callback) { var item; for(item in collection) { if(collection.hasOwnProperty(item)) { // now instead of calling console.log, // we call the callback function instead! // we can have the same behaviour as before, // but now it's infinitely more flexible callback(collection[item]); } }
}
// Let's put our function to use, supplying a callback.
// This will give us the same results as we've seen
// before.
each(someArray, function(item) { console.log(item);
});
// The benefit of callbacks is that they give
// a neat extensibility point, where a consumer
// of your API can modify functionality.
// So let's use our callback function to do exactly that
// instead of console.log()
// Remember that all we're doing with callbacks
// is passing a *reference* to a function to another
// function, as an argument. This means that we can
// pass references with anonymous functions
// (as above), variables, or named functions
// using a function reference stored in a variable
var functionReference = function(item) { console.warn(item);
};
each(someArray, functionReference);
// using a named function
function namedFunction(item) { console.error(item);
}
each(someArray, namedFunction);
// the same principle applies when the code is
// asynchronous too. It just means the callback
// happens at a later time (in fact, JS necessitates the
// use of callbacks for asynchronous code.
// Think about event handlers, setTimeout etc.
// they're basically all given callbacks, which get
// invoked in response to some event (click, timer etc)
Got your callback - Script Codes
Got your callback - Script Codes
Home Page Home
Developer Nick Williams
Username WickyNilliams
Uploaded August 27, 2022
Rating 3
Size 2,451 Kb
Views 30,360
Do you need developer help for Got your callback?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Nick Williams (WickyNilliams) Script Codes
Create amazing web content with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!