10_类.html 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. function Person(name,age){
  12. this.name = name
  13. this.age = age
  14. }
  15. /*
  16. 所有构造函数都有一个prototype 属性 这个属性指向它的原型对象
  17. 原型对象特点: 声明在原型对象下的属性和方法可以被所有的实例化对象所以共享
  18. */
  19. Person.prototype.eat = function(){
  20. console.log('吃')
  21. }
  22. console.log(Person.prototype.constructor)
  23. /*
  24. 继承父类的方法 在子类的构造函数里面 通过调用父类的.call 继承属性
  25. 子类的原型对象 = new 父类 继承方法
  26. */
  27. function Coder(name,age){
  28. Person.call(this,name,age)
  29. }
  30. Coder.prototype = new Person()
  31. Coder.prototype.constructor = Coder
  32. console.log(Coder.prototype.constructor)
  33. /* 实例化对象 */
  34. var c1 = new Coder('lisi',20)
  35. console.log(c1)
  36. c1.eat()
  37. </script>
  38. </body>
  39. </html>