Sunday, November 25, 2012

.NET basics- introduction to execution model

Metadata -
When we compile code it emits IL and Metadata. Metadata is set of table which contains information of what is defined in modules(types and members) and what module references(imported types and members).

Importance of Metadata -
Removes the dependency of header and library files when compiling. It has all the information of defined types and referenced types.
Metadata field allows an object's field to be serialized into memory block, and deserialized to create the object back.
Metadata allows garbage collector to track the lifetime of objects. Garbage collector can defined the type of the object, and from metadata it can check what all other types using this object.
Metadata is also useful for CLR's code verification process.

IL(Intermediate language) - code the compiler produced as it compiles the source code.
IL is typeless, means add instruction for 32 bit and 64 bit machine will be same.

Metadata and IL both are part of modules. CLR doesn't work with module, it works with assembly.
Assembly is logical grouping of one or more modules. There are different tools available which combines modules and create assembly out of it.

Manifest - IT is another set of metadata tables which describes files that make up assembly, exported types, resources or data files associated with the assembly.

JIT- Just in Time -
Once we write any code compiler will convert that code into IL. But the IL will not directly run on machine. This IL is first compiled into machine language by .NET just in time compiler and then will run.
you can always say the first time execution of any command on any machine will take time, but once its been run, then any further use of the same method will be fast, as instead of compiling the same method again, JIT will take the machine code directly, in order to do so, it maintains some kind of dynamic cache.

Unsafe Code - Code which is allowed to directly access the memory addresses and manipulate bytes at those addresses.

obfuscator utility - IL is very high level language. It is always possible to reverse engineer the code from given IL. If we want to distribute your assembly and dont want anyone to reverse engineer it, then you can use obfuscator utility, which will scramble the names of all private symbols in your assembly's metadata.(It is also not 100% secure, because then also its possible to create source code from IL).

FCL- Framework class libraries - set of assemblies contains thousands of type definition.
CLR - Common language runtime
CTS - Common Type System. CLR is all about types. Types exposes functionality to your application and other types. CLS defines all the types that CLR can understand. It also specifies rules for type visibility and access to the member of a type.
CLS - Common language specification. Minimum set of feature that every CLR language should implement. Because of CLS different languages can talk with each other.

Managed Code can call unmanaged function - using Kernel32.dll and user32.dll, its always possible to call unmanaged component from your managed code.
Unmanaged code can use a managed type- lot many unmanaged code requires you to supply COM component for the code to work. You can always write these COM component in managed language and them use it in unmanaged code.

Tuesday, November 6, 2012

JavaScript Aptitude Test- primitive types


Here are few examples related with primitive type(taken from one of the presentation by Siarhei Barysiuk). It will help us to understand types and their behavior in javascript.

number-
var n1 = 1;
alert(typeof n1);  //number

var n2 = 1.5;
alert(typeof n2);  //number

var n3 = 0100;
alert(typeof n3);  //number
alert(n3);         //64

var n4 = 0xFF;
alert(typeof n4);  //number
alert(n4);         //255

var n5 = 1e1;
alert(typeof n5);  //number
alert(n5);   //10

var n6 = 2e+3;
alert(typeof n6);  //number
alert(n6);   //2000

var n7 = 2e-3;
alert(typeof n7);  //number
alert(n7);   //0.002

var n8 = Infinity;
alert(typeof n8);  //number
alert(n8);   //Infinity

var n9 = 1e309;
alert(typeof n9);  //number
alert(n9);   //Infinity

var n10 = 6/0;
alert(typeof n10); //number
alert(n10);   //Infinity

var n11 = -1*Infinity;
alert(typeof n11); //number
alert(n11);   //-Infinity

var n12 = NaN;
alert(typeof n12); //number
alert(n12);   //NaN

var n13 = 10 * "string";
alert(typeof n13); //number
alert(n13);   //NaN

var n14 = 1 + 1 + NaN;
alert(typeof n14); //number
alert(n14);   //NaN

string-
var s1 = "some string";
alert(typeof s1);  //string

var s2 = 'a';
alert(typeof s2);  //string

var s3 = "10";
alert(typeof s3);  //string

var s41 = "one";
var s42 = "two"
var s43 = s41 + s42;
alert(s43);   //onetwo
var s51 = "10";
alert(typeof s51); //string

var s52 = s51 * 5;
alert(s52);   //50
alert(typeof s52); //number

var s6 = "1";
alert(typeof s6);  //string
alert(++s6);   //2
alert(typeof s6);  //number

var s7 = "some string 1";
var s71 = s7 * 1;
alert(typeof s7);  //string
alert(s71);   //NaN
alert(typeof s71); //number

boolean-
var b1 = false;
alert(typeof b1);  //boolean
var b2 = "some string";
var b21 = !b2;
var b22 = !!b2;
alert(typeof b2);  //string
alert(b21);   //false
alert(typeof b21); //boolean
alert(b22);   //true
alert(typeof b22); //boolean

All values become true when converted to a boolean, with the exception of the six falsy values
var b31 = "";
var b32 = null;
var b33 = false;
var b34 = NaN;
var b35 = undefined;
var b36 = 0;

null-
var nl1 = null;
alert(typeof nl1); //object
alert(nl1);   //null
var nl2 = 1 + null;
alert(nl2);   //1
var nl3 = 1*null;
alert(nl3);   //0

var u2 = 1 + undefined;
alert(u2);   //NaN

var u3 = 1 * undefined;
alert(u3);   //NaN

Arrays-
var a = [1,2,3];
alert(typeof a);   //object
alert(a);   //[1,2,3]
alert(a[0]);   //1
alert(a[5]);   //undefined
a[5] = "some string";
alert(a);   //[1,2,3,undefined,undefined,"some string"]
delete a[2];
alert(a);   //[1,2,undefined,undefined,undefined,"some string"]
delete a[5];
alert(a);   //[1,2,3,undefined,undefined,undefined]

var a2 = [[1,2,3],["string1","string2",3]];
alert(a2[0]);   //[1,2,3]
alert(a2[1]);   //["string1","string2",3]
var a3 = "one";
alert(typeof a3);  //string
alert(a3[0]);   //o
alert(typeof a3[0]);//string
alert(a3[1]);    //n

Monday, November 5, 2012

JQuery Basics - Understanding the insight -5

We will use following html to understand the concepts -

<div class="root">
    <div id="child1" class="div1">
        <span>
        <p class="para">Hi this is 1st para.</p>
        <p class="para">Hi this is 2nd para.</p>
        <p class="para">Hi this is 3rd para.</p>
            </span>
    </div>
    <div id="child2" class="div2">
        <p class="para" id="child2Para1">This is another paragraph1 .</p>
        <p class="para" id="child2Para2">This is another paragraph2 .</p>
        <p class="para" id="child2Para3">This is another paragraph3 .</p>
    </div>
</div>


next() - get the next sibling of matching selector. It helps us to traverse on the same level in the DOM.
Example -$('#child1 .para').next().css("color","blue");
It will apply new css on 2 paragraphs under child1 div.
What if there is no next element available? In that case it returns an empty object, we can always use length property to check if we have any elements available.
We can always use a selector in next() method to select a specific element like this -
$('#child2 .para').next('#child2Para3').css("color","blue");

We also have nextAll() method available which does the similar job for us, but the difference is, next() returns immediate next sibling, whereas nextAll() returns all the siblings. Example -
 $('#child2Para1').next() -- will return you a single object
 $('#child2Para1').nextAll() -- will return you 2 objects.

NOTE - In the above case also $('#child1 .para') itself returns us 3 objects, thus we get 2 objects if we apply next() method on it.

prev() -
It helps us to select the previous sibling of an element. It can be understood as opposite of next, int terms of selected element.
Example -$('#child2Para2').prev().css("color","blue");
it will select element before "child2para2" that is "child2para1".

parent()-
Get the parent of current element in the DOM tree. IT will return immediate parent only. Example -
$('#child2Para1').parent().css("background-color","blue");
It will help us to select the parent of "child2para1", that is div with id "child2" and will set its backgrounf to blue.
What if we want to move higher in the DOM tree, instead of sticking to the immediate parent.
Well for this jquery provides another method "parents()". it returns all the parent object of the current element.
If we use -$('#child2Para1').parents(), then it will return 2 div elements as its parent, 1) child2 and 2) root. It will also return body and html element as its parent. We can pass any selector to parents([selector]) to choose specific parent in the hierarchy.

children()-
Get the child element of current element. We can pass a selector to choose specific child as well. It will return only immediate children, not the descendant ones.
example - $('#child2').children() -  will return 3 elements
$('.root').children() -  will return 2 elements
What if we want to get descendant children as well(along with immediate children)?
Well, we can use find() method which does the similar job for us.
$('.root').find('*') - it will return all the child elements.

closest()-
takes the selector(or element or jquery object) as input and checks the current element or its parent to find the match. It is similar to parent but here are few differences between closest and parent method -
closest start search from current element, whereas parent start search from parent element.
closest return 0 or 1 result whereas parents can return 0 or many results.
closest climb up in the DOM tree, and check for the match, whereas , parents traverse the whole tree first and then filter out the matching results.

siblings()-
get the siblings of given element. Ex.  $('#child2Para2').siblings() returns 3 sibling for given element.