Arrays and Objects

Data Type: Arrays



//Defining Arrays


var languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"];


//Looping thru’ array and getting each item




for (var i=0;i<languages.length;i++)

{

    console.log(languages[i]);

}




Data Type : Object


Objects provide us with a way to represent real-world or virtual things. We can do this by storing information inside the object's properties. There are two basic ways to make objects:


Literal Notation, where we use



var Name = { };`



Constructor Notation, where we use the keyword new.


Declaration : var myObj={};


Multiple ways of Creating  Objects :



var myObj = {property1: value, property2:value};


var myObj=new Object();



Two ways of accessing Properties:



1. ObjectName.PropertyName


2. ObjectName[“PropertyName”]



Using methods



   var matt= new Object();


   matt.name=”Matt Davingly”;


matt.age=25;


matt.setAge= function(newAge)


{


   this.age = newAge;


};



Constructors


Constructors are a way to make objects with the keyword new. The most basic constructor is the Object constructor, which will make an object with no methods or properties.


For more complicated objects we can make our own constructors and put in whatever properties and methods we want.


Custom Constructors



function Person(name,age) {

  this.name = name;

  this.age = age;

}





var bob = new Person("Bob Smith", 30);



Constructors with Methods



function Rectangle(length, width) {

  this.length = length;

  this.width = width;

  this.calcArea = function() {

      return this.length * this.width;

  };

  this.calcPerimeter=function()

  {

      return 2*length+2*width;

  }

 
}


var rex = new Rectangle(7,3);

var area = rex.calcArea();

var perimeter = rex.calcPerimeter();



Array of Objects


Array of Objects can be made as below:-



// Our person constructor

function Person (name, age) {

    this.name = name;

    this.age = age;

}


// Now we can make an array of people

var family = new Array();

family[0] = new Person("alice", 40);

family[1] = new Person("bob", 42);

family[2] = new Person("michelle", 8);




Loop through the Array of Objects



function Person(name,age)

{

    this.name=name;

    this.age=age;

}


// Now we can make an array of people

family=[];

family.push(new Person("alice", 40));

family.push(new Person("bob", 42));

family.push(new Person("michelle", 8));

family.push(new Person("timmy", 6));


// loop through our new array

for (var i=0; i<family.length; i++)

{

    console.log (family[i].name);

}



Passing Objects into Functions



// Our person constructor

function Person (name, age) {

    this.name = name;

    this.age = age;

}


// We can make a function which takes persons as arguments

// This one computes the difference in ages between two people

var ageDifference = function(person1, person2) {

    return person1.age - person2.age;

}


var alice = new Person("Alice", 30);

var billy = new Person("Billy", 25);


// get the difference in age between alice and billy using our function

var diff = ageDifference(alice, billy);



List all Properties of an Object using for Loop



var nyc = {

    fullName: "New York City",

    mayor: "Michael Bloomberg",

    population: 8000000,

    boroughs: 5

};


// loop to print the value of nyc's properties

for(var property in nyc) {

  console.log(nyc[property]);

}



Extending the “Prototype”


if you want to add a method to a class such that all members of the class can use it, we use the following syntax to extend the prototype:



className.prototype.newMethod =

function() {
statements;
};


Inheritance in Class

In object-oriented programming, inheritance allows one class to see and use the methods and properties of another class.



//Animal class created here

function Animal(name, numLegs){

this.name=name;

this.numLegs=numLegs;

}




// sayName method for Animal is created as a prototype


//[this.name] inherits the name property from Animal Class

Animal.prototype.sayName=function(){

console.log("Hi my name is "+ [this.name]);

};


// Testing the constructor and method

var penguin = new Animal("Captain Cook", 2);

penguin.sayName();



The power of inheritance can be further explored here:


// This is an Animal class with sayName method

function Animal(name, numLegs) {

    this.name = name;

    this.numLegs = numLegs;

}

Animal.prototype.sayName = function() {

    console.log("Hi my name is " + this.name);

};


// defined a Penguin class with defined # of legs

function Penguin(name){

    this.name=name;

    this.numLegs=2;

}


// set its prototype to be a new instance of Animal


Penguin.prototype=new Animal();

penguin=new Penguin("Moby Dick");


//Now, inheritance is in action where penguin


//inherits sayName method from Animal class

penguin.sayName();



Using typeof variable


To execute something based on the type of variable, use “typeof” :



//Define object


var languages = {

    english: "Hello!",

    french: "Bonjour!",

    notALanguage: 4,

    spanish: "Hola!"

};


// print hello in the 3 different languages

for (var hello in languages){

    if (typeof languages[hello]==="string"){

        console.log (languages[hello]);

    }

}




Codecademy.com