31 lines
770 B
Markdown
31 lines
770 B
Markdown
# 20191221
|
|
|
|

|
|
|
|
* 原型的作用
|
|
1. 数据共享
|
|
2. 节省内存
|
|
* 实现继承 ----> 数据共享
|
|
* 通过原型进行继承
|
|
* 缺陷:因为改变原型指向实现继承,必须同时进行初始化赋值,所以导致属性重复,如果想改变属性,只有通过调用对象属性重新赋值
|
|
```js
|
|
function Person(name) {
|
|
this.name = name
|
|
}
|
|
Person.prototype.eat = function() {
|
|
console.log('真好吃')
|
|
}
|
|
// 创建学生对象
|
|
function Student(score) {
|
|
this.score = score
|
|
}
|
|
Student.prototype = new Person('张三')
|
|
Student.prototype.play = function() {
|
|
console.log('每天都在写代码')
|
|
}
|
|
var st1 = new Student(100)
|
|
console.log(st1.name)
|
|
console.log(st1.eat)
|
|
console.log(st1.play)
|
|
|
|
``` |