JavaScript - Overview


What is JavaScript ?

Javascript is a dynamic computer programming language with object-oriented capabilities. It is  used as a part of web pages, whose implementations allow client-side script to interact with the user and make dynamic pages. 

Client-side JavaScript

Client-side JavaScript is the most common form of the language. 
The JavaScript client-side mechanism provides many advantages.For example, you might use to check if the user has entered a valid e-mail address in a form field.The script should be included in or referenced by an HTML document for the code to be executed when the user submits the form, and only if all the entries are valid, they would be submitted to the Web Server.

Advantages of JavaScript

  1. Less server interaction : The code is executed on the user's processor instead of the web server thus saving bandwidth and strain on the web server.
  2. Immediate feedback to the visitors : They don't have to wait for a page reload to see if they have forgotten to enter something.
  3. Javascript is a relatively easy language
     : Javascript language is relatively easy to learn due to the syntax  close to English.

Limitations of JavaScript

  1. Client-side JavaScript issue : For security reasons reading and writing are not allowed.
  2. Reliance on End user : sometimes Javscript codes is interpreted differently by different browsers. Whereas server-side scripts will always produce the same output.
  3. Security IssuesJavascript snippets are causing security problems because they could  contains malicious code used to exploit the user's system once appended onto web pages and  executed on client servers

JavaScript development tools                               

Here we will talk about some editors that support javascript
  • Eclipse JavaScript Development Tools
  • Oracle NetBeans IDE
  • ActiveState Komodo IDE 7

JavaScript Functions Syntax


JavaScript functions are capable of more than just merely enclosing a bunch of codes while waiting for the call to execute. Functions have evolved over time leading to new definitions, execution methods and syntaxes. This post will cover some of the present roles JavaScript functions have played so far.
Function Expressions
1
2
3
4
// Function declaration
function function_name() {};
// Function expression
var function_name = function() {};
All JavaScript declarations are moved up in the scope during evaluation
1
2
function_name();//function call[WORKS]
function function_name(){};
Methods
When a function is an object’s property, it is called method. a function inside another function is also a method. This is an example for a method inside object.
1
2
3
4
5
6
var calc = {
  add : function(a,b){return a+b},
  sub : function(a,b){return a-b} 
}
console.log(calc.add(1,2)); //3
console.log(calc.sub(80,2)); //78
The add and sub functions are methods of calc object.
Example for function inside a function :
1
2
3
4
function add(a){
  return function(b){return a+b;}
}
console.log(add(1)(2)); // Output is 3
Constructors
When you add new keyword before a function and call it, it becomes a constructor that creates instances. Below is an example where constructors are used to create instances of Fruit and values are added to each Fruit‘s properties.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function Fruit(){  
  var name, family;
  this.getName = function(){return name;};
  this.setName = function(value){name=value};  
  this.getFamily = function(){return family;};
  this.setFamily = function(value){family=value};
}
var apple = new Fruit();
apple.setName("Apple");
apple.setFamily("Apple family");
var orange = new Fruit();
orange.setName ("Orange");
orange.setFamily ("Orange family");
console.log(orange.getName()); // "Orange"
console.log(apple.getName()); // "Apple"
console.log(orange.getFamily()); // "Orange family"