-
Notifications
You must be signed in to change notification settings - Fork 0
/
链式调用.html
71 lines (69 loc) · 1.56 KB
/
链式调用.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
63
64
65
66
67
68
69
70
71
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>面向对象编程----链式调用</title>
</head>
<body>
<script>
// es5写法
var CheckObject = function(obj) {
this.name = obj.name
this.password = obj.password
this.Email = obj.Email
}
CheckObject.prototype = {
checkName:function(){
// code
console.log(this, 'checkName')
return this; // 链式调用必须要返回一个this
},
checkEmail:function(){
// code
console.log(this, 'checkEmail')
return this;
},
checkPassword:function(){
// code
console.log(this, 'checkPassword')
return this;
}
}
var check = new CheckObject({
name: '小明',
password: '123456',
Email: '[email protected]'
})
check.checkName().checkEmail().checkPassword()
// es6写法
class CheckObject1 {
constructor (obj) {
this.name = obj.name
this.password = obj.password
this.Email = obj.Email
}
checkName(){
// code
console.log(this, 'checkName')
return this;
}
checkEmail(){
// code
console.log(this, 'checkEmail')
return this;
}
checkPassword(){
// code
console.log(this, 'checkPassword')
return this;
}
}
let check1 = new CheckObject1({
name: '小明',
password: '123456',
Email: '[email protected]'
})
check1.checkName().checkEmail().checkPassword()
</script>
</body>
</html>