-
Notifications
You must be signed in to change notification settings - Fork 0
/
灵魂交换.html
68 lines (57 loc) · 1.31 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>灵魂交换</title>
</head>
<body>
<h2>有两个不同的人,他们有不同的灵魂(prototype)。</h2>
<pre>
class A {
constructor(name) {
this.name = name
}
sayHi() {
return `I am ${this.name}.`
}
}
class B {
constructor(name) {
this.name = name
}
sayHi() {
return `This is ${this.name}.`
}
}
const a = new A('Jerry')
const b = new B('Lucy')
a.sayHi() // => 'I am Jerry.'
b.sayHi() // => 'This is Lucy.'
a instanceof B // => false
b instanceof A // => false
</pre>
<p>请你完成 exchange,传入两个对象,可以交换他们的灵魂:</p>
<pre>
exchange(a, b)
a.sayHi() // => 'This is Jerry.'
b.sayHi() // => 'I am Lucy.'
a instanceof B // => true
b instanceof A // => true
</pre>
<p>注意不要触碰到这两个对象原来的类,例如:</p>
<pre>
exchange(a, b)
a.sayHi() // => 'This is Jerry.'
b.sayHi() // => 'I am Lucy.'
const c = new A('Tomy')
c.sayHi() // => 应该返回 'I am Tomy.'
</pre>
<p>你也不能使用 __proto__ 属性。</p>
<script>
const exchange = (a, b) => {
}
</script>
</body>
</html>