-
Notifications
You must be signed in to change notification settings - Fork 0
/
36.原型.html
49 lines (37 loc) · 1.57 KB
/
36.原型.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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
/*
创建一个构造函数
*/
function MyClass() {
}
//向MyClass的原型中添加一个name属性
MyClass.prototype.name = "我是原型中的名字"
var mc = new MyClass()
mc.age = 18
// console.log(mc.name)
//使用in检查对象中是否会含有某个属性时,如果对象中没有但是原型有,也会返回true
//console.log("name"in mc)
//可以使用对象的hasOwnProperty()来检查对象自身中是否含有该属性
//使用该方法只有当对象自身中含有属性时,才会返回true
// console.log(mc.hasOwnProperty("age"))
/*
原型对象也是对象,所以它也要原型
当我们使用一个对象的属性或方法时,会先在自身中寻找
自身中如果有,则直接使用
如果没有则去原型对象中寻找,如果原型对象中有,则使用
如果没有则去原型的原型中寻找,直到找到Object对象的原型
Object对象的原型没有原型,如果在Object中依然没有找到,则返回undefined
*/
console.log(mc.__proto__.hasOwnProperty("hasOwnProperty"))
console.log(mc.__proto__.__proto__)
</script>
</head>
<body>
</body>
</html>