Skip to content
html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>观察者模式</title>
  </head>
  <body>
    <div class="eg2"></div>

    <script>
      // 观察者构造函数
      class Observer {
        constructor(name, fn = () => {}) {
          this.name = name
          this.fn = fn
        }
      }

      // 创建观察者
      const praepostor = new Observer('级长', (state) => {
        console.log(`因为: ${state}, 你是哪个班的`)
      })
      const teacher = new Observer('老师', (state) => {
        console.log(`因为: ${state}, 把你爸找来`)
      })
      const principal = new Observer('校长', (state) => {
        console.log(`因为: ${state}, 骂你的班主任`)
      })

      // 被观察者的构造函数
      class Subject {
        constructor(state) {
          // 记录状态
          this.state = state
          // 缓存需要通知被观察者
          this.observsers = []
        }

        // 设置状态
        setState(val) {
          this.state = val
          this.observsers.forEach((item) => {
            item.fn(this.state)
          })
        }

        addObserver(obs) {
          this.observsers = this.observsers.filter((item) => item !== obs)
          this.observsers.push(obs)
        }

        delObserver(obs) {
          this.observsers = this.observsers.filter((item) => item !== obs)
        }
      }

      // 创造一个被观察者
      const zhangsan = new Subject('学习')
      zhangsan.addObserver(praepostor)
      zhangsan.addObserver(teacher)
      zhangsan.addObserver(principal)

      const lisi = new Subject('学习')
      lisi.addObserver(teacher)
      lisi.addObserver(principal)

      console.log(zhangsan)
      console.log(lisi)
    </script>
  </body>
</html>

Layout Switch

Adjust the layout style of VitePress to adapt to different reading needs and screens.

Expand all
The sidebar and content area occupy the entire width of the screen.
Expand sidebar with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Expand all with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Original width
The original layout width of VitePress

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.

Spotlight

Highlight the line where the mouse is currently hovering in the content to optimize for users who may have reading and focusing difficulties.

ONOn
Turn on Spotlight.
OFFOff
Turn off Spotlight.