In javascript we previosly learned about the constructor pattern, which allowed us to do the following:
function Person(first, last){
this.firstName = first;
this.lastName = last;
this.greet = function () {
return "Hello, my name is " + this.firstName;
};
}
var tSwift = new Person("Taylor", "Swift");
tSwift.greet();
// => Hello, my name is Taylor
// OMG!!!
We can also make another person.
var lana = new Person("Lana", "Del Rey");
lana.greet();
We see that both lana
and tSwift
have the same greet
functionality, but if we compare their greet
methods we get a surprise.
lana.greet == tSwift.greet
// => false
This is because each time we run the new Person(...)
command we create a new object using the first
and last
name passed in, and we then set greet
to a newly created function.
Basically, we create a new greet function every time we create a new Person. That can't be good.
Luckily we have an efficient way of creating methods that doesn't require us to waste memory re-creating functions for every new object.
WE MUST USE THE PROTOTYPE!
Now we refactor our Person to use a Prototype and we will explain more about it in a moment.
function Person(first, last){
this.firstName = first;
this.lastName = last;
this.greet = function () {
return "Hello, my name is " + this.firstName;
};
}
becomes
function Person(first, last){
this.firstName = first;
this.lastName = last;
}
Person.prototype.greet = function () {
return "Hello, my name is " + this.firstName;
};
This changes nothing about how we create a person. The following works as expected.
var tSwift = new Person("Taylor", "Swift");
tSwift.greet()
Behind the scenes javascript has moved the greet
method onto a shared property called the prototype
. Now the greet
function isn't created with every new person.
lana = new Person("Lana", "Del Rey");
lana.greet == tSwift.greet
// returns true
We can check if a method or property belongs to an object or on a prototype by using hasOwnProperty
tSwift.hasOwnProperty("lastName");
// returns true
tSwift.hasOwnProperty("greet");
// returns false
- Why do we use the prototype?
- It is an efficient way of creating methods on an object.
- What is a
hasOwnProperty
?- It is property that belongs directly on object not in it's prototype.
- What is a prototype property/method?
- Any property that is in the object, but not a hasOwnProperty.
- How do we create a new
Person
?- the same way we create a person using just constructors from earlier.
Please use the prototype for creating methods.
- Create a constructor for a
superHero
that takes afirstName
andlastName
. Give each superHero asuperPunch
method that returns"WHAM!"
. - Create a constructor for a
robot
that takesname
andpurpose
. Give each robot agreet
method that returns"beep boop"
We haven't really created very advanced methods to interact with our objects. Our methods mostly return strings. In reality there are two main types of methods on an object.
A getter method returns some information about associated with our object
function Person (first, last) {
this.firstName = first;
this.lastName = last;
}
// A Getter for the firstName
Person.prototype.getFirstName = function () {
return this.firstName;
};
// A Getter for the lastName
Person.prototype.getLastName = function () {
return this.lastName;
};
// A Getter for the fullName
Person.prototype.fullName = function () {
return this.lastName + ", " + this.firstName;
};
A setter method changes one or more properties associated with an object.
function Person (first, last) {
this.firstName = first;
this.lastName = last;
}
// A setter for the firstName
Person.prototype.setFirstName = function (newFirst) {
this.firstName = newFirst;
};
// A setter for the lastName
Person.prototype.changeLastName = function (newLast) {
this.lastName = newLast;
};
// A setter for the fullName
Person.prototype.changeFullName = function (newFirst, newLast) {
this.firstName = newFirst;
this.lastName = newLast;
};
Properties associated with any object have getter and setters built-in to the same key.
var friend = {name: "joe"};
// we can get name using
friend.name
// so friend.name is also a getter
// we also set name using
friend.name = "josh";
// so friend.name is also a setter
That's kinda cool, so we won't typically have to worry too much about getters and setters.
- Make a
Starship
constructor that takes amodel
andownerName
.- Give your
StarShip
asetTopSpeed
method and agetTopSpeedMethod
that let you change and read thetopSpeed
. AssumetopSpeed
is just a number. - Give your
Starship
anaccelerateTo
method that setscurrentSpeed
to some number, unless that number is greater than itstopSpeed
.
- Give your
- Make a
Dice
constructor that takes anumberOfSides
. Add a method calledgetSide
orroll
that randomly returns a number from1
up to thenumberOfSides
.- Modify your
getSide
method to record the returned side in alastRoll
property.
- Modify your
- Make a
Radio
constructor that takes in anownerName
and asignalType
("AM" or "FM").- Add a
setStation
method to your radio that allows the following ranges:535
to1705
for "AM".88
to108
for "FM".
- Add a
listen
method that returns the following:"distorted music"
for "AM"."clear music"
for "FM".
- Add a
- Make a
Mailer
constructor that takes afrom
email address, and hascurrentMessage
set to an empty object, as well as asentMessages
array.- Add a
setSendTo
method that setscurrentMessage.sendTo
to be a specified email. - Add a
setSubject
method that setscurrentMessage.subject
to be a specified subject. - Add a
send
method that setscurrentMessage.from
to bethis.from
, pushescurrentMessage
intosentMessages
, and setscurrentMessage
to be a new object.- Modify
send
so that it checks ifcurrentMessage
has both asendTo
andsubject
before pushing tosentMessages
.
- Modify
- Add a