Objects and methods. I had a grueling day with these two today. It’s been pretty cool to see everything together using objects and methods. Essentially everything I’ve learned so far was put into the tutorials I went through today. Variables, comparison operators, if/else statements, etc. were all included. Let’s dive into some syntax. Just as a heads up, I’m not going to go through every single piece of what I learned today since that would be a seriously long post.
Methods: Codecademy defines a method as similar to a function associated with an object. It’s like setting a property, but a function comes after the equals sign instead of a string or number. Methods also have two major purposes. 1. to change object property values and 2. they can also be used to make calculations based on object properties.
var setAge = function(setAge) {
this.age = newAge;
};
//now make an object
var bob = new Object();
bob.age = 30;
bob.setAge = setAge;
//change age to 50
bob.setAge(50);
The “this” keyword above is superrrr useful. Essentially it allows you to use a method across many objects instead of just one. The keyword “this” acts as a placeholder and will refer to whichever object calls that method. Cool stuff.
More methods
var square = new Object();
square.sideLength = 6;
square.calcPerimeter = function {
return this.sideLength * 4;
};
square.calcArea = function {
return this.sideLength * this.sideLength;
};
Custom constructors help cut out all the crap. They let you create objects in one line instead of multiple and make writing code much more efficient. Check it out below.
function Car(make, model) {
this.make = make;
this.year = year;
this.model = model;
this color = black;
}
var myCar = new Car("BMW", 1985, "325e");
var dadsCar = new Car("BMW", 2004, "330zhp");
Method inside of a constructor
function Dog(adjective) {
this.adjective = adjective;
this.describeMyself = function() {
console.log("I am a " + this.adjective + " dog.";
};
var dog1 = new Dog("quick");
var dog2 = new Dog("slow");
var dog3 = new Dog("funny");
dog1.describeMyself();
dog2.describeMyself();
dog3.describeMyself();
The last three lines call the sentences to the console. And FINALLY, arrays of objects. An important thing that Codecademy wanted me to remember is that an object is just another type (like a string, number, etc). So you can make an array of objects.
function Person(name, age) {
this.name = name;
this.age = age;
}
var family = new Array();
family[0] = new Person("alice", 40);
family[1] = new Person("bob", 42);
family[2] = new Person("michelle", 8);
family[3] = new Person("timmy", 6);
//loop through array
var arrayLength = array.length;
for (var i = 0; i < arrayLength; i++) {
console.log(family[i], name);
console.log(family[i], age);
}
As you may have noticed, these have far reaching implications and add a new layer of complexity compared to prior posts. But, I am starting to believe w3schools statement when they say “In JavaScript, objects are king. If you understand objects, you understand JavaScript.” They involve so many of the concepts learned in the past couple weeks that you truly will understand JavaScript once you understand objects and methods. Tomorrow, Sunday, or Monday I’ll continue with the last section on Objects through Codecademy and can’t wait. I’ll check back in soon.
Have a great weekend!