Last active
September 5, 2017 02:33
-
-
Save simonhlee97/991ca959cc44f03b3be78213df751a14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Object oriented programming (from Khan Academy) | |
// 2 object literals | |
var winstonTeen = { | |
"nickname": "Winsteen", | |
"age": 15, | |
"x": 20, | |
"y": 50 | |
}; | |
var winstonAdult = { | |
"nickname": "Mr. Winst-a-lot", | |
"age": 30, | |
"x": 229, | |
"y": 50 | |
}; | |
var drawWinston = function(winston) { | |
fill(255, 0, 0); | |
var img = getImage("creatures/Winston"); | |
image(img, winston.x, winston.y); | |
var txt = winston.nickname + ", " + winston.age; | |
text(txt, winston.x+20, winston.y-10); | |
}; | |
drawWinston(winstonTeen); | |
drawWinston(winstonAdult); | |
// the first part of object-oriented programming: creating object types and instances of those object types. | |
// using Object oriented programming, let's re-create the above program using a Constructor Function and new Instances. | |
/* the variable name and its properties (these values will be passed in as arguments) | |
Winston | |
- nickname | |
- age | |
- x | |
- y | |
*/ | |
// the Constructor Function creates the object | |
var Winston = function(nickname, age, x, y) { | |
this.nickname = nickname; | |
this.age = age + "yrs old"; | |
this.x = x; | |
this.y = y; | |
}; | |
// new Instances of Winston | |
var winstonTeen = new Winston("Winsteen", 15, 20, 50); | |
var winstonAdult = new Winston("Mr. Winst-a-lot", 30, 229, 50); | |
// for all Winstons, run this function | |
var drawWinston = function(winston) { | |
fill(255, 0, 0); | |
var img = getImage("creatures/Winston"); | |
image(img, winston.x, winston.y); | |
var txt = winston.nickname + ", " + winston.age; | |
text(txt, winston.x+20, winston.y-10); | |
}; | |
drawWinston(winstonTeen); | |
drawWinston(winstonAdult); | |
// ------------------------------------------------------------------------------------------ | |
// Object Methods | |
var Winston = function(nickname, age, x, y) { | |
this.nickname = nickname; | |
this.age = age + "yrs old"; | |
this.x = x; | |
this.y = y; | |
}; | |
// the draw method | |
Winston.prototype.draw = function() { | |
fill(255, 0, 0); | |
var img = getImage("creatures/Winston"); | |
image(img, this.x, this.y); | |
var txt = this.nickname + ", " + this.age; | |
text(txt, this.x+20, this.y-10); | |
}; | |
// the talk method | |
Winston.prototype.talk = function() { | |
text("I'm Winston!", this.x+20, this.y+150); | |
}; | |
var winstonTeen = new Winston("Winsteen", 15, 20, 50); | |
var winstonAdult = new Winston("Mr. Winst-a-lot", 30, 229, 50); | |
winstonTeen.draw(); | |
winstonTeen.talk(); | |
winstonAdult.draw(); | |
winstonAdult.talk(); | |
// ---------------------------------------------------------- | |
// Object Inheritance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment