-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathes6_object_oriented.js
More file actions
39 lines (34 loc) · 977 Bytes
/
Copy pathes6_object_oriented.js
File metadata and controls
39 lines (34 loc) · 977 Bytes
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
typeof 123; // 'number'
typeof NaN; // 'number'
typeof 'str'; // 'string'
typeof true; // 'boolean'
typeof undefined; // 'undefined'
typeof Math.abs; // 'function'
typeof null; // 'object'
typeof[]; // 'object'
typeof{}; // 'object'
function Student(props) {
this.name = props.name || 'Unnamed';
}
Student.prototype.hello =
function() {
alert('Hello, ' + this.name + '!');
}
let jon = new Student('Jon');
jon.name; // 'Jon'
jon.hello(); // Hello, Jon!
// Here is how extend is realized in JavaScript:
function extends(Child, Parent) {
var F = function() {};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
}
function PrimaryStudent(props) {
Student.call(this, props);
this.grade = props.grade || 1;
}
extends(PrimaryStudent, Student);
PrimaryStudent.prototype.getGrade = function() {
return this.grade;
};