Skip to main content

Posts

Showing posts from April, 2019

Javascript 105 : Callbacks

Javascript is asynchronous. That means you cant guarantee that the code you write will always execute sequentially. Take the following code for example, Output When looking at the output, it is understandable that the statements has been executing sequentially.  But if I do a modification to the code like the following, the output will be different.  Output:  Don't worry about the line number 6 yet. the amendment there will just delay the the execution of the statement "console.log('statement 01')" by 100 milliseconds. Now the "statement 01" prints out at the end of the line. The change of the output can be explained as follows. the JS interpreter sees the line 6 (which is the first line of the script), and realize that it has to wait for a 100 milliseconds before executing the statement "console.log('statement 01')", but sees that other four lines has no such delay and can be executed right away. So it goes a

Javascript 104 : Closures

Closures is an easy way of implementing encapsulation in Javascript.  Suppose you need to implement the calculation of a certain employee's increment scheme. Lets simplify it by saying that the annual increment depends on a certain performance index calculated out of a certain function.  Look at the following code and try to understand what is going on.   Output: I have included a fun function called "calculatePerformanceIndex". You can completely disregard it if you want to. But if you are still confused about what are functions, you should stop now and go to the previous post in which I have explained what functions are.  "calculatePerformanceIndex" is a function to calculate the performance based on efficiency, discipline and latency. This function is completely made up by me. So don't take it serious. What you should take serious is that it returns a value and this function has been called on line 9 and initialized the "perf

Javascript 103 : Functions

A function in javascript (or any high level language) is a  block of code segment which carry out a certain task. Once created, the function is not going to do anything by its own. You have to call it in your code use its functionality. You have to master functions and all other advanced forms of it which is coming up in the later posts in order to be a good developer because you can never avoid them and it is fundamental to any development work. Unless what you develop is a "Hello world!" program! Here's how you can declare a simple function. What I have here is a simple function called "doSomething", and it will just print "Hello world" on the console. But when you run this script, no error will show up, but nothing will print in the developer console either. That is because you have to call the function in order for it to come to life and start functioning. You can call the function like this, And it will give you the out put as t