My Journey to Learning Javascript: Notes- Objects

Using real world examples, objects can be defined as a thing, like a car. The characteristics of this car such as the color and engine size could be it’s properties. The way you use the car like using the brakes, pressing the gas, or honking the horn would be the methods of the object. Methods are like functions which sometimes need information, or parameters, to work. Sometimes using one or more of the methods may change one or more of the object’s properties. For example, using the accelerator method will probably change the car’s speed property. Other properties can’t be changed: for example, the body-shape property of the car. Objects have a collection of properties and methods that can be used to manipulate certain types of data. Syntax for objects is much like that for variables except that objects have constructors.
var myDate = new Date();
To access an object’s properties you simply add a period after the object name followed by the property you want to access. For example to access an array’s length property, you simply type
arrayName.length
*This will tell you how many elements are in that array.
Accessing an objects method requires a period followed by the method(parameter). Even if the method does not have a parameter, parentheses are still needed after the method
arrayName.sort();
*this will sort the array in alphabetical or ascending order
arrayName.reverse();
*will sort the array in descending order
In JavaScript, almost “everything” is an object.
-Booleans can be objects (or primitive data treated as objects)
-Numbers can be objects (or primitive data treated as objects)
-Strings can be objects (or primitive data treated as objects)
-Dates are always objects
-Maths are always objects
-Regular expressions are always objects
-Arrays are always objects
-Functions are always objects
-Objects are objects
Object Methods
indexOf() and lastIndexOf()
are both used to find a string inside another string, also known as a substring. They take two parameters, the string you want to find and the character position where you want to start. If the position where you want to start is at zero, you do not include the second parameter. Example:
var myString = “Hello welcome to my blog”;
var stringPosition = myString.indexOf(“blog”);
console.log(stringPosition)
*would return 20 because that is where the string “blog” begins
push()
this is an array method that simply allows you to add an element to the end of your array
var coolCars=[“ferrari”,”lamborghini”,”porsche”];
coolCars.push(“roll royce”);
* will return [“ferrari”,”lamborghini”,”porsche”,“roll royce”]
concat()
allows you to join two arrays into a single new array. First you would make a new variable which will assign the first array to the second array using .concat(secondarray)
var names=[“juan”,”kristine”,”sarah”]
var ages=[24,19,21]
var concatArray= names.concat(ages)
*will return [“juan”, “kristine”, “sarah”, 24, 19, 21]
slice()
allows you to copy a portion of an array and assign it to a new variable. The parameters for slice() include the index of the portion you want to copy followed by the index marking the end of the portion you want copied.
var names=[“juan”,”kristine”,”sarah”]
var slicedArray = names.slice(0,2)
*will return [“juan”,”kristine”]
*Every(), some(), and filter() are considered to be test methods
every()
this method test whether every element in the array pass a function test. If even one element fails the test then the result is false.
var numbers = [ 1, 2, 3, 4, 5 ];
function isLessThan3(value, index, array) {
var returnValue = false;
if (value < 3) {
returnValue = true;
}
return returnValue;
}
alert(numbers.every(isLessThan3));
*this will return false because not every element is less than 3
some()
The some() method, unlike every(), will return true if any of the elements pass the test. Meaning not all have to pass,as long as even one element passes then true is given. In the above example 1,2,3 pass the test so the result is true.
filter()
using filter() creates a new array with the passing elements. So from the previous example 1,2,3 will be added to a new array if filter() is used. 4 and 5 will not be added to the new array because it does not pass the test
foreach()
foreach() executes something for each element. Foreach() does not return a value, it simply instructs a function to run for every element in an array.
var numbers = [ 1, 2, 3, 4, 5 ];
function doubleAndAlert(value, index, array) {
var result = value * 2;
alert(result);
}
numbers.forEach(doubleAndAlert);
map()
also executes a function for every element in an array, but map() will store the results to a new array.
var numbers = [ 1, 2, 3, 4, 5 ];
function multiply(value, index, array) {
var result = value * 2;
return result;
}
var doubledNumbers = numbers.map(multiply);
alert(“The doubled numbers are: “ + doubledNumbers);
*this will return [2,4,6,8,10]