我和原型链的爱恨情仇。

86 阅读1分钟
                                            |
<script>   |                                              |
| -------- | ------------------------------------------ |
|          | function Preson() {                        |
|          | this.foot = 2;                             |
|          | this.head = 1;                             |
|          | }                                          |
|          | Preson.prototype.han = function () {       |
|          | return '我是Preson的函数'                  |
|          | }                                          |
|          |                                            |
|          | function Student(name, age) {              |
|          | this.name = name;                          |
|          | this.age = age;                            |
|          | }                                          |
|          | Student.prototype = new Preson();          |
|          | console.log(Student.prototype.constructor) |
|          | Student.prototype.constructor = Student;   |
|          | console.log(Student.prototype.constructor) |
|          | let xue = new Student("王小明", 18)        |
|          | console.log(xue.name)                      |
|          | console.log(xue.foot)                      |
|          | console.log(xue.han())                     |
|          | </script>