Wednesday 16 April 2014

Inheritance and the prototype chain in JavaScript.


Although JavaScript is an object-oriented language, it is prototype-based and does not implement a traditional class-based inheritance system.
In JavaScript, each object internally references another object, called its prototype. That prototype object, in turn, has a reference to its prototype object, and so on. At the end of this prototype chain is an object with null as its prototype. The prototype chain is the mechanism by which inheritance – prototypal inheritance to be precise – is achieved in JavaScript. In particular, when a reference is made to a property that an object does not itself contain, the prototype chain is traversed until the referenced property is found (or until the end of the chain is reached, in which case the property is undefined).
Here’s a simple example:
function Animal() { this.eatsVeggies = true; this.eatsMeat = false; }

function Herbivore() {}
Herbivore.prototype = new Animal();

function Carnivore() { this.eatsMeat = true; }
Carnivore.prototype = new Animal();

var rabbit = new Herbivore();
var bear = new Carnivore();

console.log(rabbit.eatsMeat);   // logs "false"
console.log(bear.eatsMeat);     // logs "true"

Friday 7 February 2014

Javascript User Defined Duplicate function in Array

A value which you want duplicate nothing but copy of  an array or value you can use duplicate function which is user defined function in javascript

Ex: 

var a = [1,2,3,4,5];

If i want to get duplicate this array there is no function in javascript so now i am using user defined javascript function in Array.


we have Array in javascript by using this we can move forward


Array.prototype.duplicate = function() {
    return this.concat(this);
};

By running above function in console you can get a function in array just follow the below picture




var c = a.duplicate();
console.log(c);
Ouput is :[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]

Monday 13 January 2014

Java Beans (Pojo)

Actually in every project we have a Pojo classes or Beans of Java classes .Every one has doubt why we are using java bean classes with getter and setter methods . The reason we have bean class is to get and set the fields where ever we want in project . Let's see an Example

Create a java class of  Bean.
public class Bean{

   private String name;
   public String getName(){
      return name;
   }

   public void setName(String newName){
      name = newName;
   }

  
}
public class Example{

   public static void main(String args[]){
      Bean bean= new Bean();
      encap.setName("James");
     

      System.out.print("Name : " + encap.getName());
                             
    }
}
This is the class where we use the bean class here we are setting name and getting value of name .similarly you can use this in some other classes and other packages also.

Festival Wishes

Hi Guys Happy Pongal