Simplifying Objects, Inheritance and prototype in JavaScript

Although JavaScript is a Class-Free language, it supports objects creation, overriding properties/methods and inheritance. In this post, we will explore the concepts of object creation and inheritance.

In JavaScript, Objects can be created in three possible ways:

  1. An object as literal
  2. Using the function constructor
  3. Using the Object.Create method

Object as literal

A simple object Student – as an object literal – can be created as shown in the listing below:


var Student = {

    name: "dj",
    age: 32,
    Father: {
        'name': 'Ram',
        'occupation': 'service'
    },
    Subjects: [{

        name: "Physics",
        marks: 89
    },
	{

	    name: "Chemistry",
	    marks: 95
	}]
};

Above we have created an object called Student. There are three types of properties attached to Student: simple properties like name and age, complex properties like “Father”, and an array property called Subjects. We can read properties as shown in the listing below:


console.log(Student.Father.name);

for (var i in Student.Subjects) {
    console.log(Student.Subjects[i].name);
}

We can also have a function serve as the property of an object, which is known as the method. So let’s say we want to attach a method “Speak” to the Student object.  We can add the Speak method as shown in the listing below.


var Student = {

    name: "dj",
    age: 32,

};

Student.Speak = function (message) {

    var finalMessage = this.name + " has said :" + message;
    console.log(finalMessage);
};

Student.Speak("I am the best");

On calling Speak function as method, we will get output as shown in the image below:

There are some important points about the Speak method to remember:

  1. Properties can be added to an object at any time; a property can be added to an object after its creation too. For example, we added the Speak property later to Student object, rather than at the time of object creation.
  2. In the object’s method, the object is defined by value “this”. That is why we are able to print the name of the Student using this.name in the method.
  3. Calling a function as method is known as “Method Invocation Pattern”

In the above example, we are calling the Speak method on the object “Student”. So the value ofthis inside the Speak method is the Student object.

If we want to pass some other object as the value of “this” in the method, we can use the apply or call function. For example, we have one more object called Parents and,

  1. We are calling the Speak method of the Student object directly and the value of “this” in the Speak method would be Student.
  2. We are passing Parent as the value of “this” in the Student object’s Speak method. We are using the apply function to call the Student object’s Speak method and passing the Parent object as the value of this.

Read full article on the Infragistics Blog

Inheritance in Node.js

Read other posts on Node.js here

In this post we will take a look on inheritance of objects in Node.js. We will learn to use utility module to achieve inheritance in Node.js. However keep in mind that you can write plain JavaScript to achieve inheritance in Node as well. You can use Object.create() to inherit one object to another in Node.js also.

In this post we will learn to use util module to achieve inheritance in Node.js. Very first you need to import util module in your application

image

After importing util module, let us say you have an object as below,


function Student()
{

 this.name = "G Block";
 this.age = 40;
};

Just for demonstration let us add function in object using prototype,


Student.prototype.Display= function(){
 console.log(this.name + " is " + this.age + " years old");
 };

Next we are going to create ArtsStudent object which will inherit Student object.


function ArtsStudent()
{
 ArtsStudent.super_.call(this);
 this.subject = "music";
 };

util.inherits(ArtsStudent,Student);

Second line of code in ArtsStudent object is very important,

image

If you don’t call constructor of parent object as shown in above code snippet then on trying to access properties of parent object will return undefined.

In last line ArtStudent inherits Student using util.inherits() function ,

image

Next you can create instance of ArtsStudent and call function of parent object as below,


var a = new ArtsStudent();
a.Display();

Inheritance can be chained to any order. If you want you can inherit object from ArtsStudent as well. Inherited object will contain properties from both ArtsStudent and Student objects. So let us consider one more example,


function ScienceStudent()
{
 ScienceStudent.super_.call(this);
 this.lab = "Physics";
}

util.inherits(ScienceStudent,ArtsStudent);
var b = new ScienceStudent();
b.Display();

In above example ScienceStudent object inherits both Student and ArtsStudent objects. In this way you can work with inheritance in Node.js using util module. I hope you find this post useful. Thanks for reading.

Can we implement Inheritance in WCF ServiceContract : WCF Interview Series #2

Can we implement Inheritance in WCF ServiceContract?

YES we can have Contract Inheritance in WCF. In other words in WCF one ServiceContract can inherit other ServiceContract.

image

Let us take an example that you have a ServiceContract named IParentService as given below,


[ServiceContract]
 public interface IParentService
 {
 [OperationContract]
 string ParentMessage(string message);

}

Another ServiceContract named IChildService can inherit IParentService as following,


[ServiceContract]
 public interface IChildService : IParentService
 {
 [OperationContract]
 string ChildMessage(string message);
 }

Next you need to decide on implantation of Service. Single Service class can implement both contract by implementing bottom most ServiceContract in hierarchy. In this case bottom most ServiceContract is IChildService

image

Service can be implemented as following in a single service class.


public class Service1 : IChildService
 {
 public string ChildMessage(string message)
 {
 return "Hello " + message + "from Child Service";
 }

public string ParentMessage(string message)
 {
 return "Hello " + message + "from Parent Service";
 }
 }

Now you have choice that either you can expose whole hierarchy as single EndPoint or different EndPoints for different Service Contract. To expose contract hierarchy create EndPoint with bottom most ServiceContract. In this case we are creating EndPoint with Service Contract IChildService . At the client side operations from whole hierarchy could be invoked.

image

So at client side Service will be exposed as single class as given below,

image

In this way you can work with Inheritance in Service Contract. I hope you find this post useful. Thanks for reading.