购物车通过getter/setter实现

  1. class Goods {
  2. imgUrl = ''
  3. message = ''
  4. detailMsg = ''
  5. count = 0
  6. select = false
  7. num = 0
  8. show = false
  9. constructor(url: string, msg: string, detail: string) {
  10. this.imgUrl = url
  11. this.message = msg;
  12. this.detailMsg = detail
  13. }
  14. }
  15. class Box {
  16. shopname = ''
  17. goods: Goods[] = []
  18. // all = false
  19. get all() {
  20. let rs = true
  21. for (let x of this.goods) {
  22. if (x.select === false) {
  23. rs = false
  24. break;
  25. }
  26. }
  27. console.log('读取all的值为:', rs)
  28. return rs
  29. }
  30. set all(v: boolean) {
  31. console.log("设置all的值为:", v)
  32. for (let x of this.goods) {
  33. x.select = v
  34. }
  35. }
  36. constructor(shopname: string, goods: Goods[]) {
  37. this.shopname = shopname
  38. this.goods = goods
  39. }
  40. }
  41. let boxList = [
  42. new Box("鑫景辉专卖店", [
  43. new Goods("", '小米手环', "小米手环xx")
  44. ]),
  45. new Box("鑫景辉专卖1店", [
  46. new Goods("", '小米手4环', "小米1手环xx"),
  47. new Goods("", '小米手4环', "小米1手环xx"),
  48. new Goods("", '小米手4环', "小米1手环xx"),
  49. new Goods("", '小米手4环', "小米1手环xx"),
  50. ]),
  51. new Box("鑫景辉专1卖1店", [
  52. new Goods("", '小米3手3环', "小米手4环xx")
  53. ]),
  54. new Box("鑫景辉专卖2店", [
  55. new Goods("", '小米2手环', "小米手1环xx")
  56. ]),
  57. ]
  58. window.BoxList = boxList
  59. console.log(boxList)

年龄动态计算

let CurTime = '2000-01-01'

class Person {
    Birthday = '1960-01-01'
    get Age() {
        return moment(CurTime).diff(this.Birthday, 'year')
    }
}

let p = new Person()
for (let i = 0; i < 20; i++) {
    CurTime = moment('2000-01-01').add(i, 'year').format("YYYY-MM-DD")
    console.log(`当前:${CurTime},此人年龄:${p.Age}`)
}