All Articles

JavaScript - Functions

  • functions are first-class objects in JS, functions coexists with and can be treated like any other JS objects.
  • Function closures
  • Promises, which gives us better control over asynchronous code. A promise is a guarantee that eventually we will know the result of some computation. The promise can either succeed or fail, and once it has done so, there will be no more changes
//create a new promise, call resolve to explicitly resolve a promise,, reject is called to reject a promise. 
new Promise ((resolve, reject) => {});

//promise object has a *then* method that returns a promise and takes in two callbacks, a success callback and a failure callback
myPromise.then(
	val => console.log("Success"), 
	err => console.log("Error")
);
  • Maps: which we can use to create dictionary collections; and sets which allows us to deal with collections of unique items.
  • Modules: breaks code into smaller, relatively self-contained pieces that make projects more manageable.

Performance measure

console.time("My Operation");
for(var n = 0; n < maxCount; n++) {
	/* put the code to be measured here */
}
console.timeEnd("My operation");

we run the code in the for loop a certain number of times (in this case, maxCount times). Because a single operation of the code happens much too quickly to measure reliably, we need to perform the code many times to get a measurable value. When the operation ends, we call the console.timeEnd method with the same name. This causes the browser to output the time that elapsed since the timer was started.

The main difference between writing JS code like the average Joe and writing it like a JS ninja is understanding JS as a functional language.

Functions are first-class objects or first-class citizens in JS.

What’s with the functional difference?

In JS, objects can be:

  • created via literals: {}
  • assigned to variables, array entries, and properties of other objects.
  • passed as arguments to functions.
  • returned as values from functions.
  • they can possess properties that can be dynamically created and assigned.

in Js: Functions can be:

  • created via literals.
  • assigned to variables, array entries, and properties of other objects.
  • passed as arguments to other functions.
  • returned as values from functions.
  • they can possess properties that can be dynamically created and assigned.

Whatever we can do with objects, we can do the same with functions. Functions are objects just with additional, special capability of being invokable: functions can be called or invoked in order to perform an action.

Callback functions

Def: functions to be called at a later time.

//A simple callback example
var text = "Demo arigato!";
report("Before defining functions");
// Defines a function that takes a callback function and immediately invokes it
function useless(ninjaCallback) {
	report("In useless function");
	return ninjaCallback();
}
//Defines a simple function that returns a global variable
function getText() {
	report("In getText function");
	return text;
}
//Calls our useless function with the getText function as a callback
report("Before making all the calls");
assert(useless(getText) === text, "The useless function works! " + text");
report("After the calls have been made");

we can modify the above code further by defining a callback function directly as an argument

var text = 'Domo arigato!';
function useless(ninjaCallback) {
	return ninjaCallback();
}
assert(
	useless(function() { return text;}) === text,
	"The useless function works! " + text
);

Functions as Objects

//create object and assign a new property to it
var ninja = {};
ninja.name = "hitsuke";
//create a function and assign a new property to it
var wieldSword = function(){};
wieldSword.swordType = "katana";

what this allows us to do:

  • storing functions in a collection allows us to easily manage related functions — for example, callbacks that have to be invoked when something of interest occurs.
  • Memoization allows the function to remember previously computed values, thereby improving the performance of subsequent invocations.
Back