Skip to content

Commit 37e8b4e

Browse files
committed
✨ feat: 添加原型模式
1 parent 3476b68 commit 37e8b4e

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

Prototype/Prototype.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var prototype = {
2+
name: 'Reaper',
3+
hobby: {
4+
love: '123'
5+
},
6+
getName: function() {
7+
return this.name;
8+
}
9+
}
10+
11+
var obj = Object.create(prototype,
12+
{
13+
skill: {value: 'FE'}
14+
}
15+
)
16+
17+
console.log(obj.getName()); //Reaper
18+
console.log(obj.skill); // FE
19+
console.log(obj.__proto__ === prototype); //true
20+
21+
Object.create
22+
23+
let proto = {a:1}
24+
let propertiesObject = {
25+
b: {
26+
value: 2
27+
}
28+
}
29+
30+
let obj = Object.create(proto, propertiesObject)
31+
console.log(obj.__proto__ === proto); // true
32+
33+
// 方法继承
34+
let proto = function() {}
35+
proto.prototype.excute = function() {}
36+
let child = function() {}
37+
38+
// 让child 继承proto的所有原型方法
39+
child.prototype = new proto()
40+
41+
// 所有函数默认继承Object
42+
let Foo = function() {}
43+
console.log(Foo.prototype.__proto__ === Object.prototype); // true
44+
45+
// isPrototypeOf
46+
// prototypeObj 是否在obj的原型链上
47+
prototypeObj.isPrototypeOf(obj)

Prototype/Prototype.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# 原型模式
2+
3+
> 通过原型实例指定创建对象的类型,并且通过拷贝原型来创建新的对象。
4+
5+
在 JavaScript 中,实现原型模式的方法是`Object.create`方法,通过使用现有的对象来提供新创建的对象的`_proto_`
6+
7+
- 每一个函数数据类型(普通函数,类)都有一个天生自带的属性:`prototype`,并且这个属性是一个对象数据类型的值。
8+
- 并且在prototype上浏览器天生添加了一个构造函数`constructor`,属性值是当前函数(类)本身。
9+
- 每一个对象数据类型叶天生自带一个属性`proto`,属性值是当前实例所属类的原型。
10+
11+
```javascript
12+
var prototype = {
13+
name: 'Reaper',
14+
getName: function() {
15+
return this.name;
16+
}
17+
}
18+
// 同时注意 Object.create为浅拷贝
19+
var obj = Object.create(prototype,
20+
{
21+
skill: {value: 'FE'}
22+
}
23+
)
24+
25+
console.log(obj.getName()); //Reaper
26+
console.log(obj.skill); // FE
27+
console.log(obj.__proto__ === prototype); //true
28+
29+
```
30+
31+
prototype 的几种方法
32+
33+
#### 1.Object.create()方法
34+
35+
```
36+
let proto = {a:1}
37+
let propertiesObject = {
38+
b: {
39+
value: 2
40+
}
41+
}
42+
43+
let obj = Object.create(proto, propertiesObject)
44+
console.log(obj.__proto__ === proto); // true
45+
```
46+
47+
48+
49+
#### 2.方法继承
50+
51+
```
52+
// 方法继承
53+
let proto = function() {}
54+
proto.prototype.excute = function() {}
55+
let child = function() {}
56+
57+
// 让child 继承proto的所有原型方法
58+
child.prototype = new proto()
59+
```
60+
61+
#### 3.函数对Object的默认继承
62+
63+
```
64+
let Foo = function() {}
65+
console.log(Foo.prototype.__proto__ === Object.prototype); // true
66+
```
67+
68+
#### 4.isPrototypeOf
69+
70+
```
71+
prototypeObj.isPrototypeOf(obj)
72+
```
73+
74+
75+
76+
#### 5.instanceof
77+
78+
contructor.prototype是否出现在obj的原型链上
79+
80+
```
81+
obj instanceof contructor
82+
```
83+
84+
#### 6.getPrototypeOf
85+
86+
Object.getPrototypeOf(obj) 方法返回指定对象obj的原型(内部[[Prototype]]属性的值)
87+
88+
```
89+
Object.getPrototypeOf(obj)
90+
```
91+
92+
#### 7.setPrototypeOf
93+
94+
设置一个指定的对象的原型 ( 即, 内部[[Prototype]]属性)到另一个对象或 null
95+
96+
```
97+
var obj = {}
98+
var prototypeObj = {}
99+
Object.setPrototypeOf(obj, prototypeObj)
100+
console.log(obj.__proto__ === prototypeObj) // true
101+
```

0 commit comments

Comments
 (0)