1. //原型的继承 改变构造函数的原型不是继承
    2. // 给构造函数的原型的原型去继承 也就是给Object的原型
    3. // 使用create方法 创建一个新的对象 它会创建一个新的对象
    4. // function Hd() { }
    5. // console.dir(Hd);
    6. function User(age) {
    7. this.age = age
    8. this.name = 'user'
    9. }
    10. function Admin(age) {
    11. User.call(this, age)
    12. this.name = 'admin'
    13. }
    14. function Member(age) {
    15. User.call(this, age)
    16. this.name = 'member'
    17. }
    18. User.prototype.view = function () {
    19. console.log(this.age);
    20. }
    21. // Admin.prototype.role = function () {
    22. // console.log(this.name);
    23. // }
    24. // Member.prototype.role = function () {
    25. // console.log(this.name);
    26. // }
    27. // User继承Admin的方法
    28. // 继承的方法 1 直接在构造函数的原型的原型
    29. // 方法一
    30. // Admin.prototype.__proto__ = User.prototype
    31. // Member.prototype.__proto__ = User.prototype
    32. // 方法二 使用setPrototypeOf
    33. // Object.setPrototypeOf(Member.prototype, User.prototype)
    34. // Object.setPrototypeOf(Admin.prototype, User.prototype)
    35. // 方法三 使用Object.create方法实现 使用这种方法的问题就是 它会生成一个新的对象,并且将该对象的原型指向
    36. // 这个函数的原型
    37. Admin.prototype = Object.create(User.prototype)
    38. Member.prototype = Object.create(User.prototype)
    39. // // 将constructor的属性设置为不可遍历的参数
    40. // Object.defineProperty(Admin.prototype, 'constructor', {
    41. // value: Admin,
    42. // enumerable: false
    43. // })
    44. // Admin.prototype.role = function () {
    45. // console.log(this.name);
    46. // }
    47. // Member.prototype.role = function () {
    48. // console.log(this.name);
    49. // }
    50. // 方法四
    51. // 使用call方法来继承 寄生式组合继承
    52. // 使用Object.create 方法和call方法还有apply方法来实现继承,但是也必须使用defineProperty方法来将constructor的值设置为不可遍历的属性
    53. let member = new Member(12)
    54. let admin = new Admin(21)
    55. // console.dir(admin);
    56. // // admin.role()
    57. // // member.role()
    58. member.view()
    59. admin.view()
    60. // console.log(member.);