index.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. /**
  12. * es5
  13. */
  14. function Phone(brand,price){
  15. this.brand = brand
  16. this.price = price
  17. }
  18. Phone.prototype.call = function(){
  19. console.log('dadianhua')
  20. }
  21. let huawei = new Phone('华为',5000)
  22. huawei.call()
  23. console.log(huawei)
  24. /**
  25. * es6
  26. */
  27. class Phone1{
  28. constructor(brand,price){
  29. this.brand = brand
  30. this.price = price
  31. }
  32. call(){
  33. console.log('es6 dadianhua ')
  34. }
  35. }
  36. let onePlus = new Phone1('1+',1999)
  37. console.log(onePlus)
  38. /**
  39. * es5 继承
  40. */
  41. function sonPhone(brand,price,color,size){
  42. Phone.call(this,brand,price)
  43. this.color = color
  44. this.size = size
  45. }
  46. // 继承原型
  47. sonPhone.prototype = new Phone
  48. sonPhone.prototype.constructor = sonPhone
  49. sonPhone.prototype.photo = function(){
  50. console.log('take')
  51. }
  52. sonPhone.prototype.playGame = function(){
  53. console.log('play')
  54. }
  55. const chuizi = new sonPhone('chuizi',4999,'black','6.5')
  56. console.log(chuizi)
  57. /**
  58. * es6类继承
  59. */
  60. class sonPhone1 extends Phone1{
  61. constructor(brand,price,color,size){
  62. super(brand,price) // xxx.call(this,brand,price)
  63. this.color = color
  64. this.size = size
  65. }
  66. photo(){
  67. console.log('take')
  68. }
  69. playGame(){
  70. console.log('play')
  71. }
  72. //子类重写
  73. call(){
  74. console.log('xiaomi call')
  75. }
  76. }
  77. const xiaomi = new sonPhone1('xiaomi',2999,'red','5.5')
  78. console.log(xiaomi)
  79. /**
  80. * class中的getter&setter
  81. * 对象中的属性进行绑定
  82. * 类似php魔术方法
  83. */
  84. class Phone2{
  85. get price(){
  86. console.log('read price')
  87. return 'hi'
  88. }
  89. set price(val){
  90. console.log('change price')
  91. }
  92. }
  93. let p = new Phone2()
  94. console.log(p.price)
  95. p.price = 20
  96. </script>
  97. </body>
  98. </html>