Overview: Today we are going to go over a variety of things, much of it deals with the inner workings of Javascript itself. We will take a look at functions, objects and inheritance, some shorthand shortcuts we can use, as well as how to use jQuery AJAX better.
See the Pen functions as first class - assignable by Mike P (@mikedemo) on CodePen.
See the Pen functions as fist class - returnable by Mike P (@mikedemo) on CodePen.
See the Pen functions as first class - passable by Mike P (@mikedemo) on CodePen.
See the Pen functions as first class - as properties of objects by Mike P (@mikedemo) on CodePen.
See the Pen using callbacks by Mike P (@mikedemo) on CodePen.
longRunningFnand passing it the
notifyfunction and the string
<br>Process is done!<br>, these are the callback function and its parameters that we are going to execute in the
longRunningFnfunction when it completes (in this case instead of an HTTP call we will do a 3 second timer). In the mean time, we see our Javascript does not wait for the timer to complete before executing the next line of code, the
doOtherStufffunction. So how will we ever know when the original
longRunningFnfunction completes? Thats where callback functions come in to save the day, if it wasn't for the callback function in
longRunningFn, we would never know if that function completed or not.
See the Pen bad jquery async example by Mike P (@mikedemo) on CodePen.
hasAccessvariable to false, then check if we have access. We are going to a random API that returns some data, and we are saying that as long as some data is returned, access is allowed. This API will 100% of the time return data, so in theory my
hasAccessvariable will become true, and the button will never be disabled.
checkAccessfunction fires right away, however the conditional
if (hasAccess)will execute before our
checkAccessfunction has a chance to complete. jQuery's
ajaxfunction has you covered here, but there is a few ways to do handle this.
See the Pen better jquery async by Mike P (@mikedemo) on CodePen.
ajaxfunction to help us with our asynch problems. The property
successwill execute (if it has been assigned a function) upon a successful response from the server. So, we caa just do our disabling logic in there right? Yes and no. It solves the problem, but not in a very maintainable way as far as the code is concerned. What if the logic that needs to be done to enable/disable the button becomes much more complex? What if that
successfunction itself needs to execute an AJAA call? Our code is going to look really bad, and our function that makes the AJAX call will not be reusable since its result has specific logic coded into it.
successfunction, but where can it go? And how can we make this AJAX call more re-usable?
See the Pen promise async example by Mike P (@mikedemo) on CodePen.
ajaxwill return a Promise interface, referred to sometimes as a deferred object. We will talk more about deferred objects and Promise's in a more advanced tutorial, just know these are special types of objects. Because jQuery
ajaxreturns an object, we can assign it to a variable. The jQuery
ajaxdeferred object has a method called
.thenthat will not execute until the deferred object has completed its task. We can utilize
.thenas well as the fact that we can assign our
ajaxfunction to a variable to make our code simple and clean. The function that makes the AJAX call is re-usable now, since it simply returns as a deferred object with the HTTP response in it, and we have kept the logic we need to run on the request data seperate.
See the Pen inheritance by Mike P (@mikedemo) on CodePen.
prototype chainor
prototypical inheritance. As objects inherit each other, each of their new functionlties is added to the
prototype chain.
function Person (name, gender, age) {and then assigning all of the passed in values to properties I am declaring of the object. Using this constructor is part of what makes this object a 'prototype' and not just any other object or function.
Object.prototype.property = value;such as in
Developer.prototype.typeCode = function () { }
callfunction that is run on the parent object in the child object's constructor is essentially like calling the superclass/parentclass's constructor in normal class based languages.
See the Pen order not gaurenteed by Mike P (@mikedemo) on CodePen.
See the Pen use array of objects instead for order by Mike P (@mikedemo) on CodePen.
See the Pen using not not by Mike P (@mikedemo) on CodePen.
See the Pen ternary and shorthand bad example by Mike P (@mikedemo) on CodePen.
See the Pen ternary and shorthand good by Mike P (@mikedemo) on CodePen.
See the Pen truthy and falsy by Mike P (@mikedemo) on CodePen.