-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
54 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,66 @@ | ||
#### 原型,原型链 | ||
|
||
##### 一,是什么? | ||
函数的 prototype 属性指向了一个对象,这个对象正是调用该构造函数而创建的实例的原型。 | ||
那什么是原型呢?你可以这样理解:每一个JavaScript对象(null除外)在创建的时候就会与之关联另一个对象,这个对象就是我们所说的原型,每一个对象都会从原型"继承"属性 | ||
|
||
```js | ||
function Fun(){} | ||
function Person(){} | ||
const obj = {}; | ||
//### prototype | ||
// 每一个函数都有一个 prototype 属性, | ||
// 虽然写在注释里,但是你要注意: | ||
// prototype是函数才会有的属性 | ||
Person.prototype.name = 'Kevin'; | ||
|
||
let fn = new Fun(); | ||
//原型 | ||
fn.__proto__ = Fun.prototype; | ||
Fun = Fun.prototype.constructor; | ||
var person1 = new Person(); | ||
var person2 = new Person(); | ||
console.log(person1.name); | ||
console.log(person2.name); | ||
|
||
//原型链 | ||
Fun.prototype.__proto__ = Object.prototype ; | ||
console.log(person1.__proto__); | ||
console.log(person1.__proto__ === Person.prototype) //true | ||
console.log(Object.getPrototypeOf(person1) === Person.prototype) //true | ||
console.log(Person === Person.prototype.constructor) //true | ||
``` | ||
关系图(构造函数,实例原型,实例) | ||
|
||
![image](https://user-images.githubusercontent.com/26174343/230852617-92b3eda4-5740-4a0c-9f35-9febf2980b47.png) | ||
|
||
|
||
##### 二,实例与原型 | ||
当读取实例的属性时,如果找不到,就会查找与对象关联的原型中的属性,如果还查不到,就去找原型的原型,一直找到最顶层为止。 | ||
|
||
Object.prototype.__proto__ = null | ||
##### 三,原型的原型 | ||
![image](https://user-images.githubusercontent.com/26174343/230856959-8c41dfdb-202a-48b1-b1bb-738e194f3802.png) | ||
|
||
##### 四,原型链 | ||
Object.prototype 的原型是 null | ||
``` | ||
// Object 顶层对象 | ||
console.log(Object.prototype.__proto__ == null);//true | ||
``` | ||
null 表示“没有对象”,即该处不应该有值。查找属性的时候查到 Object.prototype 就可以停止查找了。 | ||
所以图中由相互关联的原型组成的链状结构就是原型链,也就是蓝色的这条线。 | ||
![image](https://user-images.githubusercontent.com/26174343/230861564-647b5a32-f7f7-49c5-bbe3-83c8bbdfcb0e.png) | ||
|
||
##### 五,补充 | ||
###### constructor | ||
``` | ||
function Person() {} | ||
var person = new Person(); | ||
console.log(person.constructor === Person); // true | ||
//当获取 person.constructor 时,其实 person 中并没有 constructor 属性,当不能读取到constructor 属性时, | ||
//会从 person 的原型也就是 Person.prototype 中读取,正好原型中有该属性,所以: | ||
console.log(person.constructor === Person.prototype.constructor); // true | ||
``` | ||
###### __proto__ | ||
__proto__ ,绝大部分浏览器都支持这个非标准的方法访问原型,然而它并不存在于 Person.prototype 中,实际上,它是来自于 Object.prototype , | ||
与其说是一个属性,不如说是一个 getter/setter,当使用 obj.__proto__ 时,可以理解成返回了 Object.getPrototypeOf(obj)。 | ||
|
||
|
||
|
||
|
||
##### 二,能做什么?继承, | ||
|