How to we organize our code?
How to we organize our code?
How do we group our application into parts that can be thought of as independent?
How to we organize our code?
How do we group our application into parts that can be thought of as independent?
How do we make reusable code, that can be extended into more complex systems?
A collection of functions and properties
// Dog Properties
var firstName = "Fido"
var lastName = "Destroyer of Carpets"
// Dog Functions
function bark() {
console.log("Bark!")
}
function sayName (firstName, lastName) {
console.log("This dog's name is" + firstName + " " + lastName);
}
A collection of functions and properties
// Dog Object
var fido = {
firstName : "Fido",
lastName : "Destroyer of Carpets",
bark : function() {
console.log("Bark!")
},
ruinCarpet : function (carpet) {
carpet.status = "ruined";
carpet.color = "stained";
}
}
// Dog Object
var fido = {
firstName : "Fido",
lastName : "Destroyer of Carpets",
bark : function() {
console.log("Bark!")
},
ruinCarpet : function (carpet) {
carpet.status = "ruined";
carpet.color = "stained";
}
}
// Another Dog
var mimi = {
firstName : "MiMi",
lastName : "SquirrelMenace",
bark : function () {
console.log("Bark!")
},
ruinCarpet : function (carpet) {
carpet.status = "ruined";
carpet.color = "stained";
}
}
What happens when we want another dog?
var ClassName = function(){}
The Constructor Invocation Pattern
//Create a class
var ClassName = function(propertyName, propertyName2){
// attach arguments as properties to "this", a special variable
this.propertyName = propertyName;
this.propertyName2 = propertyName2;
}
// A class for Dog
var Dog = function(firstName, lastName) {
this.firstName = firstName
this.lastName = lastName
}
//Create a class
var ClassName = function(propertyName, propertyName2){
// attach arguments as properties to "this", a special variable
this.propertyName = propertyName;
this.propertyName2 = propertyName2;
}
ClassName.prototype.methodName = function() {
console.log(this.propertyName);
}
// Dog Class
var Dog = function(firstName, lastName) {
this.firstName = firstName
this.lastName = lastName
}
// A Bark Method
Dog.prototype.bark = function () {
console.log("Bark!")
}
//Create an "instance" of the class
var instance = new ClassName("something", "something else");
var anotherInstance = new ClassName("something completely", "different");
instance.methodName();
anotherInstance.methodName();
// Dog Class
var Dog = function(firstName, lastName) {
this.firstName = firstName
this.lastName = lastName
}
// Bark Method
Dog.prototype.bark = function () {
console.log("Bark!")
}
Dog.prototype.checkCollar() {
console.log("This dog's name is " + this.firstName + " " + this.lastname);
}
// Dog Instance
var fifi = new Dog("Fifi", "Destroyer of Carpets");
fifi.bark();
fifi.checkCollar();
A collection of functions and variables
// Dog Properties
var firstName = "Fido"
var lastName = "Destroyer of Carpets"
// Dog Functions
function bark() {
console.log("Bark!")
}
function sayName (firstName, lastName) {
console.log("This dog's name is" + firstName + " " + lastName);
}
Reusable code
var Dog = function(firstName, lastName) {
// Dog Properties
this.firstName = firstName
this.lastName = lastName
}
Dog.prototype.bark = function () {
console.log("Bark!")
}
Dog.prototype.checkCollar = function () {
console.log("This dog's name is" + this.firstName + " " + this.lastName);
}
this
Reusable code
var fido = new Dog("Fido", "Destroyer of Carpets");
var fifi = new Dog("Fifi", "Destroyer of Carpets");
fido.bark();
fifi.bark();
fido.checkCollar();
fifi.checkCollar();
Group related code
Group related code
Assign responsibilities to Objects
Group related code
Assign responsibilities to Objects
Have objects keep track of their own State
Group related code
Assign responsibilities to Objects
Have objects keep track of their own State
Define methods of interaction between objects
Group related code
Assign responsibilities to Objects
Have objects keep track of their own State
Define methods of interaction between objects
Don't let objects depend on the existence of each other!