Skip to main content

Posts

Showing posts from April, 2013

Extending jQuery library

The beauty of jQuery is that is extensible. You can easily extend the jQuery function or its alias $. For e.g. $.fn.disable = function() {   return this.each(function() { if(typeof this.disabled != 'undefined') this.disabled = true; } ); } The above code creates a function which will disable the element and return the element collections so that you can cascade more jQuery funtions e.g $("input").disable(); The above code disables all input type elements.

window.onload vs jQuery doucment ready

Any script which manipulates DOM structure needs to wait till the document has loaded. The traditional way to do this was to use window.onload method e.g. window.onload = funcerion () { alert("document loaded"); } However the above method has drawbacks. It will wait till the entire document is loaded including images. Now if a web page is image heavy, the above alert won't be displayed until all the images have loaded. Ideally you want the function to execute once the DOM structure is ready. An alternative is jQuery document ready method e.g. $(document).ready(function() { alert("DOM loaded");}); The above function can also be written as $(function() { alert("DOM loaded"); }); The beauty of ready method is that you can have more than one for the same document and they all will execute in the order they are written. You can only have one window.onload method for one document.

LINQ Sort Operators - IEnumerable and IOrderedEnumerable

Consider following example var query = Process.GetProcessess(). .OrderBy(p => p.WorkingSet64) .ThenOrderBy(p => p.ThreadCount) In this case query is of type IOrderedEnumerable Hence you cannot user a Where Operation like this query = query.Where(p => p.ProcessName.Length < 10 You will get a compile error that you cannot IEnumerable to IOrderedEnumerable However if you add Where operator on first query, it will return IEnumaerable e.g var query = Process.GetProcessess(). .OrderBy(p => p.WorkingSet64) .ThenOrderBy(p => p.ThreadCount) .Where(p => p.ProcessName.Length < 10)

Document Object Model in IE vs Other browsers

It is intersting to see that document object model in IE considers all comments outside HTML tags as valid elements where as other browsers seems to be ignoring it Tryout  in a html document in IE with atleast one comment element outside html tag. IE reports one element more compared to other browsers. There is no such thing as Standard!!!!! Everyone is creating its own Standard

LINQ Filtering Operators

LINQ filtering operators are extension methods on IEnumerable and IQueryable . There are two types of filtering operators 1. OfType 2. Where OfType returns an filtered list of IEnumerable and works on IEnumerable also. E.g ArrayList myArray = new ArrayList { "one", 1, new object(), "two", 2 }; var stringOnly  = myArray.OfType ().Where(i => i.Length > 2); The above code filters only string types and returns as IEnumerable on which you can apply Where filter.

Implicit type in C#

C# 3.0 introduced implicit type declaration var. This is not same as var type in JavaScript which is a weak type. The variable is still strongly type however the type is inferred by the compiler based on the value. E.g. var s = "Joe"; will result in s a string type. Similarly var i = 10; will result i as int type. However there are certain restrictions on declaring var types 1. Implicit local variable must be initialized. e.g. var j; will result in error 2. Implicit typed local variable cannot  have multiple declarators e.g. var i, j=0; is not allowed 3. Cannot assign null to an implicit typed local variable e.g var i = null; is not allowed 4. Cannot impicitly convert string to int e.g. var i = "43"; int j = i+ 1; will result in error; You can use implicit type to create Anonymous types e.g var person = new {Name="Joe",Age=37}; Console.WriteLine("{0} : {1}",person.Name, person.Age); Compiler will create class with Read onl