Wednesday, November 2, 2016

Prototypes objects constructors the basics

Prototypes is a more efficient way to use methods inside of constructor functions.  It allows you to create methods that can be called for each instance of a particular constructors.

Meaning that:

function Monsters(name) = {
  this.name = name;
  this.health = 100;
  this.takeAHit = function() {
    this.health --;
  }
}

The above takeAHit function has to be run for each time we make a monster.

var bigfoot = new Monsters(bigfoot);
var sharkweek = new Monsters(sharkweek);

if there where thousands of names it would take more computing power and more time call the takeAHit() function for each new name.

Wouldn't it be nice if we could call it once for all of them?

Prototype is what you need!

function Monsters(name) = {
  this.name = name;
  this.health = 100;
  this.takeAHit;
  }
}
Monsters.prototype.takeAHit = function() {
    this.health --;
   }

Constructor Functions


  • describes how an object should be created.
  • simler objects
  • each created object is known as an instance of that object...

This is an example of a Constructor Function.  
function Monster(rank) {  // the "M" Monster tells other its a constructor Function.
  this.rank = rank;  // explains what the function name or rank is
  this.health = 100;  // starting heath is 100
  this.takeHit = function() { // a method that takes damage to the heath 
    this.health--;
  }
  this.isDead = function() {  // when he dies return the health.
    return this.health <= 0;
  }
}

var monster = new Monster("Captain");  // a new instance of the Constructor
var badGuy = new Monster("bigfoot"); // a new instance of the Constructor

Another:


function Dice(sides) {
  this.sides = sides,
  this.roll = function () {
    var randomNumber = Math.floor(Math.random() * this.sides) + 1;
    return randomNumber;
  }
}

Make another Method Calc

Making a calculator isn't so hard.  Lets take a look.

var calculator = {
    sum: 0,
    add: function(value) {
    this.sum += value;
  },
    subtract: function(value) {
    this.sum -= value;
  },
    multiply: function(value) {
    this.sum = this.sum * value;
  },
    divide: function(value) {
    this.sum = this.sum / value;
  },
    clear: function() {
    this.sum = 0;
  },
    equals: function() {
    return this.sum;
  }
}

creating my first method. Math.floor() Math.random()

a method is something we've been using all along.  an example is:

console.log()
console is the object
.log() is the method it prints to the console anything in the ();

another example is:

document.write()
document is the object determined by javascript and the html connected to it.
write() writes info to the html document.

This is an example of writing my first method

var dice6 = { //dice6 is the object name
  sides: 6,  //it has a property with the value set to 6
  roll: function () {  // this is the declared method with an anonymous function()   
    var randomNumber = Math.floor(Math.random() * this.sides) + 1;
// this creates a local scope variable called randomNumber
// it uses some methods to help.  The Math.floor() tells it to round down.
// Math.random()  generates a random number between 0 and .999
// it is multiplied by the amount of sides that has been previously set. At sides: 6 using this.sides.
// we add one to make sure we can use use the full range 1-6
  console.log(randomNumber
  }
}

var dice100 = {
  sides: 100,
  roll: function () {
    var randomNumber = Math.floor(Math.random() * this.sides) + 1;
    console.log(randomNumber);
  }
}

For Loops and For In loops

a for loop lets you iterate through and array or object.

example:

var array = [1, 2, 3, 4];

for (var i = 0; i < array.length; i ++) {
  console.log(i);
}

// this console.logs the placement/key/prop of i in the array 0, 1, 2, 3 not the value of 1, 2, 3, 4;

for (var i = 0; i < array.length; i ++) {
  console.log(array[i]);
}

//this console.logs the value of each key so this array console.logs 1, 2, 3, 4;

the special for in loop is only for arrays;

var obj = { name: 'josh', age: '34', haircolor: 'black', address: {street: '1012 old mountain rd', city: 'Denver', country: 'USA'} } for (var prop in obj) { console.log(prop) }

//this console.logs the keys of the object asking for the name of the key
name
age
haircolor
address



for (var prop in obj) { console.log(obj[prop]) }
//this console.logs the values of the object asking for the object's property.
josh
34
black
Object {street: "1012 old mountain rd", city: "Denver", country: "USA"}

for (var prop in obj) {
  console.log(obj[prop].city)
  //  this iterates through the object values then looks for the value of a key named city
}



Tuesday, November 1, 2016

Arrays join() turns into string - concat() merges two arrays together - indexOf() find the key index of

The array join method is useful when you want to join the contents in an array in a single string.  Take the following array:

var weekdays = ["mon", "tues", "w", "thu", "fri"];

the .join() method would do the following.

var workDays = weekdays.join(", ");

This joins the array together and adds a ",  " between each value in the array.

concat() is another useful tool in javascript arrays.

concat() join's to arrays together to create one array.  see the following:

var weekends = ["sat", "sun"];

var allDays = weekdays.concat(weekends);

This creates a var allDays = ["mon", "tues", "w", "thu", "fri", "sat", "sun"].

indexOf() works well to find out the position "index" of a given value.

var sat = allDays.indexOf("sat");

This would return 5.

var frat = allDays.indexOf("frat");
This would return -1 because there isn't a fraturday in the list.

Array pop() shift()

if you wanted to remove the last item in an array you would want to use pop() method. This removes the last item from an array but also returns it.

example:

var array = [1, 2, 3];
var last = array.pop([2]);

This removes the last item from the array "3" then save's 3 in the var last.  If you wanted to use it later.

Similarly the shift() method does the same for the beginning of an array.