C4 Week2
C4 Week2
C4 Week2
This slide deck consists of slides used in 2 lecture videos in Week 3. Below
is a list of shortcut hyperlinks for you to jump into specific sections.
http://www.dj4e.com/code/javascript-objects
http://www.dj4e.com/code/javascript-objects.zip
Definitions
• Class - a template - Dog
• Method or Message - A defined capability of a
class - bark()
• Attribute - A defined data item in a class - color
• Object or Instance - A particular instance of a
class - Lassie
http://en.wikipedia.org/wiki/Object-oriented_programming
Terminology: Class
http://en.wikipedia.org/wiki/Object-oriented_programming
Terminology: Instance
http://en.wikipedia.org/wiki/Object-oriented_programming
Terminology: Method
http://en.wikipedia.org/wiki/Object-oriented_programming
Objects in JavaScript
• The OO pattern in JavaScript is a little different.
• The function is indeed a store and reuse pattern.
• The function keyword returns a value which is
the function itself - it makes a function!
First-Class Functions
http://en.wikipedia.org/wiki/First-class_function
A Sample Class
This is the template for
js01.htm
function PartyAnimal() {
making PartyAnimal
this.x = 0; objects.
this.party = function () { Each PartyAnimal
this.x = this.x + 1; object has a bit of data.
console.log("So far "+this.x);
} Each PartyAnimal object
}
has a bit of code.
an = new PartyAnimal();
Create a PartyAnimal
an.party(); object
an.party();
an.party(); Tell the object to run the
party() code. js01.htm
js-01.htm
function PartyAnimal() { an
this.x = 0;
this.party = function () {
x:
this.x = this.x + 1;
}
console.log("So far "+this.x);
party()
}
an = new PartyAnimal();
So far 1
an.party();
So far 2
an.party();
So far 3
an.party();
js-01.htm
Object Life Cycle
•http://en.wikipedia.org/wiki/Constructor_(computer_science)
Object Life Cycle
• Objects are created, used, and discarded
• Constructors are implicit in JavaScript - natural
• A constructor in a class is a special block of
statements called when an object is created
• Destructors are not provided by JavaScript
http://en.wikipedia.org/wiki/Constructor_(computer_science)
js03.htm
js03.htm
function PartyAnimal() {
this.x = 0;
console.log("In the 'constructor'");
this.party = function () {
this.x = this.x + 1;
console.log("So far "+this.x);
}
}
an = new PartyAnimal();
In the 'constructor'
So far 1
an.party();
So far 2
an.party();
So far 3
an.party();
Many Instances
• We can create lots of objects - the class is the
template for the object.
• We can store each distinct object in its own variable.
• We call this having multiple instances of the same
class.
• Each instance has its own copy of the instance
variables.
js04.htm
function PartyAnimal(nam) {
this.x = 0;
Constructors can have
additional parameters. These
this.name = nam;
console.log("Built "+nam);
can be used to set up instance
this.party = function () { variables for the particular
this.x = this.x + 1; instance of the class (i.e., for
console.log(nam+"="+this.x); the particular object).
}
}
s = new PartyAnimal("Sally");
s.party();
Built Sally
Sally=1
j = new PartyAnimal("Jim"); Built Jim
j.party(); Jim=1
s.party(); js04.htm Sally=2
s
js04.htm
function PartyAnimal(nam) {
this.x = 0;
x:
this.name = nam; name:
console.log("Built "+nam);
this.party = function () {
this.x = this.x + 1;
console.log(nam+"="+this.x);
}
}
s = new PartyAnimal("Sally");
s.party();
Built Sally
Sally=1
j = new PartyAnimal("Jim");
j.party();
s.party(); js04.htm
s
js04.htm
function PartyAnimal(nam) {
this.x = 0;
x:
this.name = nam; name:
console.log("Built "+nam);
this.party = function () {
this.x = this.x + 1; j
console.log(nam+"="+this.x); x:
}
} name:
s = new PartyAnimal("Sally");
s.party();
Built Sally
Sally=1
j = new PartyAnimal("Jim"); Built Jim
j.party(); Jim=1
s.party(); js04.htm Sally=2
Definitions
• Class - a template - Dog
• Method or Message - A defined capability of a
class - bark()
• Object or Instance - A particular instance of a
class - Lassie
• Constructor - A method which is called when the
instance / object is created
Summary