Sunday, March 21, 2010

Object Oriented JavaScript Part2 -- "this"

People experienced with some Object Oriented Programming language like Java and Python must be very familiar with using "this" pointer. JavaScript also involves a lot of uses with the "this" pointer.

The first question that we usually need to solve is that "Where does the 'this' pointer exist?". This is a rather simple question with JavaScript. The "this" pointer exists within any "Function" object ( As some of people already know that "function" is an instance of the global "Object" object in JavaScript; therefore, we will usually say the "Function" object. ). Code below will exhibit how a function is declared in JavaScript:


function myFunction(arg1) {
console.log(arg1);
}

These lines of code simply declares a Function object and one can simply just add a line like this this.name = "myName"; within this function. As we can see, "this" pointer exists within the function definition


I guess the most confusing part is to figure out what the "this" pointer points to? A rule of thumb to define what the "this" points to is that " 'this' pointer points to the object that contains the function where the 'this' exists within ". Within the code above 'this' pointer points to the 'window' object if that function is declared in the web page globally

Friday, March 19, 2010

Stack And Heap

I believe that there are lots of developers who do not have a computer science degree and did not go from the basic understanding of computers to programming languages like me. I am neither a computer scientist nor a software engineer. Developing web application is totally just my hobby and I self-taught everything except the basic function-based c++ programming style.

I didn't understand the computer memory completely; therefore, when dealing with deep memory allocation problem, i used to just do it based on my feeling to it. A lot of Java developers think that they do not have to deal with memory allocation because of the fantastic garbage collector feature. However, better understanding the garbage collector would help yourself with dipping into the language even more. Some languages that must have a host like Javascript also need the developers to understand memory allocation if you ever consider that speeding up your code is very essential.

Computer memory used for programming is usually divided into 3 parts. Code container (what I call), Stack and Heap. Code container contains the code that your compiler compiles your programming code to. When you program a project, the code is usually in English. However, when the code went through the compiler, it will become machine code.

Stack area in the memory is the place where variables with static size are stored in. Stack follows the Last In First Out rule(LIFO), meaning that the last memory created in the Stack will be removed the first. Usually, those variables are primitive values like int, double, float and so on. (However, when you use primitive wrapper type like in Javascript, you can do "var myVar = new Boolean(true);", this variable will be considered as a reference or pointer pointing to the actual Boolean object which exists in the heap). Because primitive values usually have a static size, they are placed in the Stack area.

Heap area in the memory is the place where variables with dynamic size are stored in. The reason why we call that dynamic size is that size of values in the Heap is allowed to be determined at run time. Usually, with Object Oriented Languages, actual objects are stored in the Heap and object pointers are stored in the Stack. Therefore, when you create two pointers and copy the one pointer to the other one like "obj1* p1 = new Obj(); obj1* p2 = p1" in C++, you are just copying the pointer from p1 to p2 and changes made to the object "p1" points to will directly affect the object that "p2" points to because they are actually pointing to the same object on the Heap.

I love cars just as much as loving web development. The garbage collection feature is good and convenient but just like comparing automatic and manual transmission on the cars, automatic is a convenient feature but it is never as fast as driving the manual one. The garbage collection feature is convenient but it has to search for memory that will not be used then remove it, it makes the process a bit slower.