Skip to main content

Posts

Showing posts from April, 2016

Javascript: the module pattern

The module pattern is defined by two things. 1-An outter wrapping function 2-An returned object containing at least one function It was initially introduced a decade ago by Douglas Crockford in order to provide encapsulation, therefore exposing only what is needed, reducing the risk users would access, undocumented features.  

Javascript: Closure and Lexical Scope

The two are separate topics, I just happen to put them in the same post for now. Closure Closure is the capability to remember it's lexical scope, even when executed outside it's lexical scope. If two inner functions, are within the same scope, they share the same closure.     Lexical scope The lexical scope is the scope in which a variable is declared during the first pass of the javascript compilation, which has two passes, compilation and execution. During the compilation pass, the compiler goes through the code looking for formal declaration, that is, var, function and parameters of function. For each of these formal declarations, the compiler will check the current lexical scope, and add this identifier if it isn't already there. Then at the second pass, execution, the compiler will run the code as we are intuitively use to and once it reaches the the identifiers found earlier, will ask if they are left hand side(RHS), or right hand side (LHS). If th

Javascript: The 'this' keyword

Here is a summary of the 4 rules defining the value of 'this': 1-Was the function called with the new keyword? If so, use that object. 2-Was the function called explicitly with call or apply? Use the passed in object. 3-Was the function called implicitly with the dot operator? Use the owning object, also called base object or context object, which means the object to the left of the dot of the function invocation. 4-If none of the above, the default rule applies, 'this' is the global object, except if in strict mode, 'this' is set to undefined.    

Javascript: It`s misunderstood concepts

Javascript gained in popularity in the last decade following Google`s V8 engine then client side frameworks like AngularJs and server side development in NodeJs. According to StackOverflow`s 2015 survey, Javascript is the most used programming language. Yet many still compare using this language to dancing in the minefield. Javascript does have many concepts that are often misunderstood. For example the `this` keyword, doesn't behave the same as in C, C++, or Java. Coming from these language means almost forgetting anything we know about 'this' when learning how 'this' behaves in Javascript. Other misunderstood concepts of Javascript are: Scope, proto chains, hoisting and closures. I will summarize some of these key concepts in the following blog posts.