Friday, February 17, 2017

JavaScript Object Oriented and Functional Programming



Declare JavaScript Objects as Variables:

//The constructor function is called Car.
var Car = function() {
  this.wheels = 4;
  this.engines = 1;
  this.seats = 5;
};

//In order to use the constructor function, you will be required to create an instance.  In this case, our instance is myCar.  The keyword "new" is required to when calling a constructor.
var myCar = new Car();

//Now, once the myCar  instance is created, it can used like any other object and have its properties accessed and modified.  For example:
myCar.nickname = "twin";

//After running the code above, you will have the following properties in myCar instance.
{"wheels":4, "engines":1, "seats":5, "nickname":"twin"}


Make Object Properties Private

//Here the private property is gear.  The value is initially set to 4 once the object has been instantiated.
//The constructor function is called Bike.
var Bike = function() {
  var gear = 4;
  
  this.setGear = function(changeGear){
    gear = changeGear;
  };
  this.getGear = function(){
    return gear;
  };
  
  return gear;

};

var myBike = new Bike();


Iterate over Arrays with map (Reference: Mozilla Developer Network)
// Predefined Array declared with values
var oldArray = [1,2,3,4,5];
var newArray = oldArray;

//Using the map method.  It will iterate through every element of the array just like a for loop would.
var newArray = oldArray.map(function(val){
  return val + 3;
});


Condense arrays with reduce

//To use reduce you pass in a callback whose arguments are an accumulator (in this case, previousVal) and the current value (currentVal).  The accumulator is like a total that reduce keeps track of after each operation. The current value is just the next element in the array you're iterating through.

//The array method reduce is used to iterate through an array and condense it into one value.var array = [4,5,6,7,8];var singleVal = 0;  singleVal = array;

//This array filter function will add all values in the array.singleVal = array.reduce(function(previousVal,currentVal){  return previousVal + currentVal;},0);


Filter Arrays with filter

//The filter method is used to iterate through an array and filter our elements where a given condition is not true.  The filter method is used to iterate through an array and filter out elements where a given condition is not true. filter is passed a callback function which takes the current value (we've called that val) as an argument.  Any array element for which the callback returns true will be kept and elements that return false will be filtered out.
var oldArray = [1,2,3,4,5,6,7,8,9,10];
var newArray = oldArray;

//This array filter function will only select values below 6.
newArray = oldArray.filter(function(val){
  return val < 6;
});

Sort Arrays with sort

var array = [1, 12, 21, 2];
//This function will sort the array from largest to smallestarray.sort(function(a,b){  return b - a;});

Reverse Arrays with reverse

var array = [1,2,3,4,5,6,7];
var newArray = [];newArray = array;
newArray = array.reverse();

Concatenate Arrays with concat
//concat 
can be used to merge the contents of two arrays into one.
var oldArray = [1,2,3];var newArray = [];var concatMe = [4,5,6];
newArray = oldArray.concat(concatMe);

No comments:

Post a Comment