Javascript

Array slice

Array slice

The slice() method returns a shallow copy of a portion of an array into a new array object selected from beginning to end (end not included). The original array will not be modified.

//              0         1        2          3 
var animal = ['Tiger','Rabbit','Elephant','Peigon']; 
var animal_slice = animal.slice(1,3); 
document.write(animal); 
document.write('<br/>'); 
document.write(animal_slice);

Output
Tiger,Rabbit,Elephant,Pigeon
Rabbit, Elephant

code

Java Script functions

In javascript, there are three ways to declare functions.

  1. Named expression

    function f(){
     console.info("I am a named expression");
    }
    f();
  2. Anonymous expression

    var f1 = function(){
     console.info("I am a anonymous expression");
    }
    f1();
  3. Mixing both – hybrid approach

    var f22 = function f2(){
    console.info("m\"ixing both - hybrid approach");
    }
    f22();

Code 

In javascript functions are objects that can be invoked/initialize

//Function as a object

var f3= function(){
 console.info("I am a testing function");
}

f3.greet = "Hello World!";
f3();

console.info(f3.greet);

More Examples

// Object Literal notation

var obj = {
 id:100,
 getId:function(){
 return this.id;
 }
};

console.info(obj.getId())

//Function as an object - Constrcution function

var obj1 = function (fName,lName){ //or var obj1 = function Student(fName,lName){ 
 this.fName = fName;
 this.lName = lName;
 
 this.getFullName = function(){
 return this.fName+' '+this.lName;
 }
}

var std = new obj1('Nuwan','Chathuranga');
console.info(std.getFullName())