Tuesday, September 25, 2012

Object Oriented Programming Concept in JavaScript

JavaScript is dynamically type language unlike C# and VB which are statically typed. It means compile time validations are available in statically typed language(C#, VB) which is not possible in dynamically typed languages (JavaScript, python.Etc)

Object creation and instantiation are slightly different in JavaScript. Unlike Java, C++, C# etc, JavaScript does not contain class statement. JavaScript is a prototype-based language. JavaScript uses functions as classes.

There are 2 ways for creating and Instantiating a class in JavaScript.
1. Constructor function
//Creating a StudentCls class
function StudentCls (){
};
//Defining properties of StudentCls class
function StudentCls ()
{
this.name=’Bimlesh’;
this.id=’20001’;
};


//Defining Method inside StudentCls class
function StudentCls ()
{
this.name=’Bimlesh’;
this.id=’20001’;
this.PersonalInfo=function()
{
alert(‘Hi I am ‘+this.name);
}
};

//Object creation and properties, method access.
var objStu1=new StudentCls();
objStu1.name;
objStu1. PersonalInfo();
var objStu2=new StudentCls();
objStu2.name;
objStu2. PersonalInfo();
//Using a constructor function
function StudentCls (name,id)
{
this.name=name;
this.id=id;
this.PersonalInfo=function(message)
                          {
                          alert(message+this.name);
                          }
};

//Object creation and method access.
var objStu=new StudentCls(‘Bimlesh Singh’,’20001’);
objStu. PersonalInfo(‘Hi I am’);

2. Literal notation:
//Creating a StudentCls class
var StudentCls= {
};

//Defining properties of StudentCls class
var StudentCls= {
name:’Bimlesh’,
id:’20001’
};

//Defining Method inside StudentCls class
var StudentCls= {
name:’Bimlesh’,
id:’20001’,
PersonalInfo: function()
                    {
                 alert(‘Hi I am ‘+this.name);
                   }

};

//Object creation and properties, method access.
StudentCls.name;
StudentCls. PersonalInfo();
//Calling parameterized methods.
var StudentCls= {
name:’Bimlesh’,
id:’20001’,
PersonalInfo: function(message)
                     {
                    alert(message+this.name);
                     }
};

//Object creation and properties, method access.
StudentCls. PersonalInfo(‘Hi I am’);

Note: if you are using literal notation like below
var StudentCls= {
name:’Bimlesh’,
id:’20001’,
PersonalInfo: function(message)
                     {
                     alert(message+this.name);
                     }
};

var obj= StudentCls.name;
alert(obj);//it will work correctly

but if you are using this approach to method then you will get error saying ‘undefined’
var obj= StudentCls. PersonalInfo(“Hi I am”);
alert(obj);//it will through error.

//Correct approach use either .call or .apply method.
var obj= StudentCls. PersonalInfo;

alert(obj.call(StudentCls,’Hi I am’));
Note.for calling overloaded method we need to set first parameter name as class name and then values.
While using .apply instead of .call we need to pass array as second parameter.
var obj= StudentCls. PersonalInfo;
alert(obj.call(StudentCls,[’Hi I am’]));

UseFul Links:
1. http://net.tutsplus.com/tutorials/javascript-ajax/the-basics-of-object-oriented-javascript
2.http://viralpatel.net/blogs/object-oriented-programming-with-javascript

3. http://www.javascriptkit.com/javatutors/oopjs4.shtml

Thursday, September 13, 2012

What’s the difference between ‘Dynamic’, ‘Object’ and reflection?

Many developers think that ‘Dynamic’ objects where introduced to replace to ‘Reflection’ or the ‘Object’ data type. The main goal of ‘Dynamic’ object is to consume objects created in dynamic languages seamlessly in statically typed languages. But due to this feature some of its goals got overlapped with reflection and object data type.
Eventually it will replace reflection and object data types due to simplified code and caching advantages. 

Dynamic object is a small feature provided in the DLR engine by which we can make calls to objects created in dynamic languages. The big picture is the DLR which helps to not only to consume, but also your classes can be exposed to dynamic languages.

Object / Reflection
Reflection and Object type is only meant for referencing types whose functions and methods are not know during runtime. Reflection and object type do not help to expose your classes to other languages. They are purely meant to consume objects whose methods are known until runtime

What is Dynamic Type Or define DLR?


Design Time/Compile Time Object:      In .net  programming we can access all the properties and functions at compile time only as illustrated in below example where we are accessing the method of Calculator class(calc.Add(10,20)).
e.g.
Calculator calc = GetCalculator();
Int sum = calc.Add (10, 20);
Run Time Object /Late Binding: In this Case compiler doesn’t checks for compile time error everything is handled at run time. The most important thing is that we can’t access the properties and methods while writing code.

If you wanted to do the exact same thing as in e.g. (like if it were some other class, maybe old-COM interop, or something where the compiler didn't know a priori that Add() was available, etc) you'd do this:
e.g.
object calc = GetCalculator ();
Type calcType = calc.GetType();
object res = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, new object[] { 10, 20 });
Int sum = Convert.ToInt32 (res);


Before .net 4.0 we are handling any COM Component (dynamically typed languages) as mentioned in e.g. .



We store the COM objects in object Type or in short with the help of reflection and Object Type we till now manage our code while dealing with dynamically typed languages.


DYNAMIC TYPES OR DLR:
DLR (Dynamic language runtime) is set of services which add dynamic programming capability to CLR. DLR makes dynamic languages like LISP, Javascript, PHP,Ruby to run on .NET framework.
Due to DLR runtime, dynamic languages like ruby, python, JavaScript etc can integrate and run seamlessly with CLR. DLR thus helps to build the best experience for your favorite dynamic language. Your code becomes much cleaner and seamless while integrating with the dynamic languages.
E.g. We can write the same code as mentioned in e.g. as below with dynamic Type.


dynamic calc = GetCalculator();
int sum = calc.Add(10, 20);

Please Note here GetCalculator is a COM component method not of the .Net class.

Please follow below links for detail.
http://www.codeproject.com/Articles/42997/NET-4-0-FAQ-Part-1-The-DLR
http://www.hanselman.com/blog/C4AndTheDynamicKeywordWhirlwindTourAroundNET4AndVisualStudio2010Beta1.aspx

 

Wednesday, September 12, 2012

How can we consume an object from dynamic language and expose a class to dynamic languages?

To consume a class created in DLR supported dynamic languages we can use the ‘Dynamic’ keyword. For exposing our classes to DLR aware languages we can use the ‘Expando’ class.

So when you want to consume a class constructed in Python , Ruby , Javascript , COM languages etc we need to use the dynamic object to reference the object. If you want your classes to be consumed by dynamic languages you need to create your class by inheriting the ‘Expando’ class. These classes can then be consumed by the dynamic languages.

For more details please visit below link;
http://www.codeproject.com/Articles/42997/NET-4-0-FAQ-Part-1-The-DLR

Friday, September 7, 2012

What is Optional and Named parameters in C# 4.0?

Optional Parameters: Function containing default values in its declration which can be called with less number of parameters.
e.g.
public static string CheckTicket( bool _isVIP=false,string name="Unknown")
{
//Body of function..}//Above Function can be called like..
}
1. Program.CheckTicket();
2. Program.CheckTicket(true);
3. Program.CheckTicket(true,"Bimlesh");

Named Parameters:  while calling function this provides an option for passing function parameters with any sequence.

e.g.
public static string CheckTicket( bool _isVIP=false,string name="Unknown")

{//Body of function..
}
//Above Function can be called like..
1. Program.CheckTicket(name: "Bimlesh",_isVIP:true);

2. Program.CheckTicket(name:"Bimlesh");

                                                          Sample Code