-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathObject basics.html
62 lines (57 loc) · 1.14 KB
/
Object basics.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<title>Object basics</title>
</head>
<meta charset="utf-8">
<body>
Object basics
</body>
<script>
//对象创建方法1
var person = {
name: ['Bob', 'Tom'],
age: 16,
gender: 'male',
interests: ['music', 'dota2'],
son: {
boy: 'Jack',
girl: 'lucy'
},
bio: function() {
alert(this.name[0] + ' ' + this.age + ' ' + this.gender + ' ' + this.interests[0]);
},
greeting: function() {
alert('Hi, ' + this.son.girl);
}
};
person.bio();
person.greeting();
person.run = function(argument) {
alert(argument);
};
person.run('pdc');
//对象创建方法2
function createPerson(name) {
var obj = {};
obj.name = name;
obj.greeting = function() {
alert('Hi ' + this.name);
};
return obj;
};
var person1 = createPerson('lucy');
var person2 = createPerson('tom');
person1.greeting();
person2.greeting();
//对象创建方法3
function Person(name) {
this.name = name;
this.greeting = function() {
alert('Hi2 ' + this.name);
}
};
var person3 = new Person('jack');
person3.greeting();
</script>
</html>