-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path21-constructorFunctionAndMethods.html
42 lines (33 loc) · 1.23 KB
/
21-constructorFunctionAndMethods.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Demo</title>
<script type="text/javascript">
// Adding methods to our objects
// Constructor function
var car = function(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
this.ageOfCar = carAgeCalc; // no parens
}
// Function (method) to calculate age
var carAgeCalc = function() {
//var currentDate = new Date().getFullYear();
var numYears = 2013 - this.year;
return numYears;
}
// Vars that contain the properties of the objects
var myCar = new car ("Toyota", "RAV4", 2003);
var wifeCar = new car ("Toyota", "Sienna", 2009);
var firstCar = new car ("Ford", "Fiesta", 1988);
</script>
</head>
<body>
<script type="text/javascript">
document.write("My " + myCar.make + " " + myCar.model + " is " + myCar.ageOfCar() + " years old."); // don't forget parens
</script>
</body>
</html>