Tuesday, October 6, 2015

Runtime Javascript anonymous functions

Javascript anonymous functions are function that are dynamically declared at runtime.
In other way function can be created without function name,
Javascript nomal function(with name) will be created using function declaration
But, Anonymous function uses function operator.


Here’s a example of a ordinary function:
  1. function abc()
  2. {
  3.   alert("Created by function declaration");
  4. }
  5. abc();
Here’s the same example created as an anonymous function:
  1. var abc = function()
  2. {
  3.   alert("Created by function operator");
  4. }
  5. abc()
Above code, anonymous function is assigned to a variable abc. Javascript function is also a object like any other object. Hence function objects also can be assigned or passed as parameter.


Sunday, October 4, 2015

Javascript prototype is easy

  1. Javascript prototype is quite fuzzy word for javascript developers in the beginning.
  2. Prototype is the property which is available for all javascript functions by default.
  3. This property helps to attain inheritance in javascript.
  4. As most of them  aware of classical inheritance concept i.e inherits the methods and properties from parent class.
  5. But, In javascript there is no class keyword to achieve inheritance.
  6. Javascript is function based object orient programming language.

Find the following simple snippet to understand prototype.

function abc() {
}

Prototype methods and property created for function abc

abc.prototype.testProperty = 'Hi, I am prototype property';
abc.prototype.testMethod = function() { 
   alert('Hi i am prototype method')
}

Creating new instances for function abc

var objx = new abc();

console.log(objx.testProperty); // will display Hi, I am prototype property

objx.testMethod();// alert Hi i am prototype method

var objy = new abc();


console.log(objy.testProperty); //will display Hi, I am prototype property

objy.testProperty = Hi, I am over-ridden prototype property

console.log(objy.testProperty); //will display Hi, I am over-ridden prototype property

Same way prototype methods can be overridden in the instance

Note: If you are still not able to understand please comment your query.