Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules":
Tell us about yourself.
Goal: Understanding objects
Goal: Understanding functions
Goal: Understanding "this"
In computer science, an object is a location in memory having a value and possibly referenced by an identifier. An object can be a variable, function, or data structure. [source: Wikipedia]
var carObject = {
make: 'Volkswagen',
model: 'Rabbit',
drive: function() {
// Code that makes car go forward
}
};
When we talk about objects, we're interested in two main concepts:
var carObject = {
make: 'Volkswagen', // attribute
model: 'Rabbit', // attribute
drive: function() { // method
// Code that makes car go forward
}
};
Think about these geometric shapes:
circle, square, rectangle, star, rhombus, triangle
Imagine going shopping for a new car.
In object-oriented terms, we say that you have purchased an instance of the class of objects known as cars.
Let's imagine another way of shopping for a new car.
In object-oriented terms, we say that you have purchased an instance of a car based off of a prototype, from which it inherited some attributes and methods.
In JavaScript, we use a constructor function to initialize new objects, and we use the new keyword to call the constuctor.
// Create a constructor function for new cars
function Car() {
// Set attributes of all new cars
this.odometer = 0;
}
var myCar = new Car();
// myCar can access properties we defined in Car
console.log(myCar.odometer); // 0
// Create a constructor function for new cars
function Car(make, model, year) {
// Set the given attributes of this object
this.make = make;
this.model = model;
this.year = year;
// Set attributes of all new cars, independent of variables
this.odometer = 0;
}
var myCar = new Car('Volkswagen', 'Rabbit', 2006);
// myCar can access properties we defined in Car
console.log(myCar.odometer); // 0
console.log(myCar.make); // Volkswagen
The prototype is the collection of all the attributes and methods that the object knows about.
The prototype property of an object is used primarily for inheritance: methods and attributes on a function's prototype property are available to instances of that function.
function Car() {
this.odometer = 0;
}
// Extend the prototype to define methods for new Cars
Car.prototype.drive = function() {
this.odometer = this.odometer + 1;
};
// Instantiate a new Car
var myCar = new Car();
// myCar can access methods defined in the prototype
console.log(myCar.odometer); // 0
myCar.drive();
console.log(myCar.odometer); // 1
It's common practice for constructor function names to start with an uppercase letter.
It's common practice for regular function names to start with a lowercase letter.
// capitalized function "Car"? This must be a constructor
function Car() {
this.odometer = 0;
}
// lowercase function "drive"? This must be a regular function
Car.prototype.drive = function() {
this.odometer = this.odometer + 1;
};
// Create a constructor function for new cars
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
this.odometer = 0;
}
// Extend the prototype to define methods for new cars
Car.prototype.drive = function() {
this.odometer = this.odometer + 1;
};
// Instantiate a new car
var myCar = new Car('Volkswagen', 'Rabbit', 2006);
Let's make our own constructors and objects! We'll be using an exercise put together by GDI Chicago:
Suppose you wanted an easy way to talk about different types of cars.
We can create a constructor function for each sub-type of Car that inherits attributes and methods from Car.
// Assume Car() is defined as before
function Hatchback(make, model, year, doors) {
// Call the Car constructor on the attributes
// Important: "this" is the first argument
Car.call(this, make, model, year);
// Set Hatchback-specific properties
this.doors = doors;
}
// Tell the Hatchback to extend the Car object
Hatchback.prototype = Object.create(Car.prototype);
We can also create methods specific to Hatchback.
function Hatchback(make, model, year, doors) {
Car.call(this, make, model, year);
this.doors = doors;
}
Hatchback.prototype = Object.create(Car.prototype);
// Now add a Hatchback-specific function
Hatchback.prototype.openHatch = function() {
console.log('You opened the hatch!');
}
var myHatch = new Hatchback('Volkswagen', 'Rabbit', 2006, 3);
myHatch.drive();
myHatch.openHatch();
Substitutability is a principle of object-oriented programming introduced by BarbaraLiskov in a 1987 conference keynote entitled "Data abstraction and hierarchy".
In a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S without altering any of the desirable properties of that program.
Let's extend our Video to make a MusicVideo! We'll be using another exercise put together by GDI Chicago:
When creating an object using our Car constructor, we have to pass in our arguments in a very specific order: make, then model, then year.
var myCar = new Car('Volkswagen', 'Rabbit', 2006);
This is annoying and hard to maintain.
There is a better way.
Replace your list of arguments with a single argument.
// The better way
function Car(args) {
this.make = args.make;
this.model = args.model;
this.year = args.year;
}
// Instantiate your object
// NOTE: You can now list the arguments in any order!
var myCar = new Car({
model: 'Rabbit',
year: 2006,
make: 'Volkswagen'
});
You can also provide default values for your arguments within the constructor.
// Now, with defaults!
function Car(args) {
args = args || {};
this.make = args.make || 'Unknown';
this.model = args.model || 'Unknown';
this.year = args.year || 0;
}
// Instantiate your object
// NOTE: Now there's a backup if you don't know a value
var myCar = new Car({
make: 'Volkswagen',
year: 2006
});
Let's clean up our constructors using what we learned! We'll be using another exercise put together by GDI Chicago:
You can create objects that know how to interact with your website.
In a real-world situation, you would probably create your car objects using data imported from a database!
Once your website code is working correctly, it's a good idea to go back and refactor it. Refactoring is a process developers use to restructure their existing code to make it easier to read.
Although it takes time to refactor your code now, right now is when you're most familiar with what you wrote.
Let's refactor some JavaScript using objects! We'll be using another exercise put together by GDI Chicago:
Please help us make this class better by completing this short survey: