12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- <!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('吃')
- }
- console.log(Person.prototype.constructor)
- /*
- 继承父类的方法 在子类的构造函数里面 通过调用父类的.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>
|