Repository navigation
Expand file tree
/
Copy path06.02.lesson.html
More file actions
163 lines (122 loc) · 3.46 KB
/
Copy path06.02.lesson.html
File metadata and controls
163 lines (122 loc) · 3.46 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CPIT405 - JavaScript</title>
</head>
<body>
<h1>Object Oriented Programming</h1>
<script language="JavaScript" type="text/javascript">
/**
* Declaring an object
*/
var profile1 = {
name: 'Khalid',
age: 22,
job: 'Salesman',
talk: function () {
alert("Hello, I'm " + this.name + " " + this.age + " years old.");
}
};
profile1.talk();
var profile2 = {};
profile2.name = 'Khalid';
profile2.age = 22;
profile2.job = 'Salesman';
profile2.talk = function () {
alert("Hello, I'm " + this.name + " " + this.age + " years old.");
};
// Using the Object constructor:
var profile3 = new Object();
profile3.name = 'Khalid';
profile3.age = 22;
profile3.job = 'Salesman';
profile3.talk = function () {
alert("Hello, I'm " + this.name + " " + this.age + " years old.");
};
var day1 = {
squirrel: false,
events: ["work", "touched tree", "pizza", "running", "television"]
};
console.log(day1.squirrel); // false
console.log(day1.wolf); // undefined
day1.wolf = false;
console.log(day1.wolf); // false
var anObject = {left: 1, right: 2};
console.log(anObject.left); // 1
delete anObject.left;
console.log(anObject.left); // undefined
console.log("left" in anObject); // false
console.log("right" in anObject); // true
var obj1 = {value: 10};
var obj2 = obj1;
var obj3 = {value: 10};
console.log(obj1 == obj2); // true
console.log(obj1 == obj3); // false
obj1.value = 15;
console.log(obj2.value); // 15
console.log(obj3.value); // 10
/**
* Object-Oriented Programming
*/
var Car = function (make, year) {
this.make = make;
this.year = year;
this.is_turned_on = false;
};
Car.prototype.drive = function () {
if (this.is_turned_on)
console.log(this.make, this.year, "is driving right now!");
else
console.log("Start car engine before drive!");
};
Car.prototype.startEngine = function () {
this.is_turned_on = true;
console.log("Car engine is started!");
};
var c = new Car("VW", "2006");
c.drive();
c.startEngine();
/**
* Class inheritance
*/
var Person = function (firstName) {
this.firstName = firstName;
};
Person.prototype.sayHello = function () {
console.log("Hello, I'm " + this.firstName);
};
Person.prototype.walk = function(){
console.log("I am walking!");
};
var person1 = new Person("Ali");
var person2 = new Person("Fahd");
person1.sayHello();
person2.sayHello();
var Student = function (firstName, subject) {
// Call the parent constructor, making sure (using call)
// that "this" is set correctly during the call
Person.call(this, firstName);
this.subject = subject;
};
Student.prototype = Object.create(Person.prototype);
// Set the "constructor" property to refer to Student
Student.prototype.constructor = Student;
// Override the "sayHello" method
Student.prototype.sayHello = function(){
console.log("Hello, I'm " + this.firstName + ". I'm studying "
+ this.subject + ".");
};
Student.prototype.sayGoodBye = function(){
console.log("Goodbye!");
};
var student1 = new Student("Mohammad", "Information Technology");
student1.sayHello();
student1.walk();
student1.sayGoodBye();
// Check that instanceof works correctly
console.log(student1 instanceof Person); // true
console.log(student1 instanceof Student); // true
</script>
</body>
</html>