123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- function Person(name,age){
- this.name = name
- this.age = age
- }
- /*
- 所有的构造函数下面都有一个prototype 属性
- 这个属性指向它的原型对象
- 原型对象的特点: 声明在原型对象下面的属性和方法 都被实例化对象所共享
- 属性写在 构造函数里
- 方法写在 原型对象下
- */
- Person.prototype.eat = function(){
- console.log(this.name + 'eat')
- }
- var p1 = new Person('zs',30)
- p1.eat()
- console.log(p1)
- /*
- 继承父类的属性 在子类的构造函数里面 通过调用父类的.call 去继承
- 子类的继承对象 = new 父类 继承方法
- */
- function Coder(name,age){
- Person.call(this,name,age)
- }
- /* 继承父类的方法 */
- Coder.prototype = new Person()
- Coder.prototype.constructor = Coder
- console.log(Coder.prototype.constructor)
- var c1 = new Coder('lisi',20)
- console.log(c1)
- c1.eat()
- </script>
- </body>
- </html>
|