RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1238280
Accepted
Михаил Камахин
Михаил Камахин
Asked:2022-01-31 22:24:23 +0000 UTC2022-01-31 22:24:23 +0000 UTC 2022-01-31 22:24:23 +0000 UTC

摆脱类实例在字段控制台中的可见性

  • 772

我有一个ThemeRadioJavaScript 课程。
初始化类的实例时

const themeRadio = new ThemeRadio('.radio-theme');
console.log(themeRadio);

而当你输出到控制台时,你可以看到字段matchMediaDarkListener,你怎么能方便地摆脱它呢?
要检查收音机是否默认,您需要更改应用程序的主题,这可以在 Windows 中完成(选择默认应用程序模式)。

在此处输入图像描述

class ThemeRadio {
  constructor(selectorRadios) {
    const nodeListBool = NodeList.prototype.isPrototypeOf(selectorRadios);
    this.themeRadiosDOM = nodeListBool ? selectorRadios : document.querySelectorAll(selectorRadios);
    this.matchMediaDark = window.matchMedia('(prefers-color-scheme: dark)');

    this.addEventsListenerRadio(this.themeRadiosDOM, 'input', (event) => {
      const target = event.target;
      const value = target.value;

      // localStorage.setItem('theme', JSON.stringify(value));
      this.choosingDesiredTheme(value);
    });

    const localStorageExist = this.initRadioValueFromLocalStorage(this.themeRadiosDOM);
    
    if (!localStorageExist) {
      const theme = this.getRadioValue(this.themeRadiosDOM);
      // localStorage.setItem('theme', JSON.stringify(theme));
      this.choosingDesiredTheme(theme);
    }


  }

  addEventsListenerRadio(domElems, eventName, listenerFunc) {
    for (let i = 0; i < domElems.length; i++) {
      const item = domElems[i];
      item.addEventListener(eventName, listenerFunc);
    }
  }

  addTheme(themeName, excludeClasses = ['dark-theme', 'light-theme']) {
    const classesDOM = Array.from(document.documentElement.classList);
  
    for (const className of classesDOM) {
      const findClassName = excludeClasses.find(excludeClass => className === excludeClass);
  
      if (findClassName) {
        document.documentElement.classList.remove(findClassName);
      }
    }
  
    document.documentElement.classList.add(themeName);
  }

  choosingDesiredTheme(theme) {
    if (theme !== 'default-theme') {
      this.matchMediaDark.removeEventListener('change', this.matchMediaDarkListener);
      this.addTheme(theme);
    } else if (theme === 'default-theme') {
      const themeDefault = this.getDefaultTheme();
      this.matchMediaDark.addEventListener('change', this.matchMediaDarkListener);
      this.addTheme(themeDefault);
    }
  }

  getDefaultTheme() {
    return this.matchMediaDark.matches ? 'dark-theme' : 'light-theme';
  }

  matchMediaDarkListener = (event) => {
    const darkActive = event.matches;
    if (darkActive) {
      this.addTheme('dark-theme');
    } else {
      this.addTheme('light-theme');
    }
  }

  initRadioValueFromLocalStorage(radioDomElems) {
    // const radioArrElems = Array.from(radioDomElems);
  
    // const theme = JSON.parse(localStorage.getItem('theme'));
  
    // if (theme) {
    //   const findRadio = radioArrElems.find(radio => radio.value === theme);
    //   if (findRadio) {
    //     findRadio.checked = true; 
    //   }
    //   this.choosingDesiredTheme(theme);
    //   return true;
    // }
    return false;
  }

  getRadioValue(domElems) {
    const arrElems = Array.from(domElems);
    const activeRadio = arrElems.find(elem => elem.checked);
    return activeRadio.value;
  }
}

const themeRadio = new ThemeRadio('.radio-theme');
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  --transition-timing-function: linear;
  --transition-duration: 0.2s;
  --header-bg-color: rgb(250, 250, 251);
  --header-color: inherit;
  --main-bg-color: rgb(132, 239, 255);
  --main-color: inherit;
  --footer-bg-color: rgb(249, 249, 255);
  --footer-color: inherit;
}

.dark-theme {
  --white: rgb(231, 231, 231);
  --header-bg-color: rgb(0, 0, 0);
  --header-color: var(--white);
  --main-bg-color: rgb(37, 82, 88);
  --main-color: var(--white);
  --footer-bg-color: rgb(66, 66, 66);
  --footer-color: var(--white);
}

html {
  height: 100%;
}

body {
  margin: 0;
  font-family: 'Roboto', sans-serif;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-height: 100%;
}

img {
  display: block;
  max-width: 100%;
}

p {
  margin: 10px 0;
}

.container {
  display: block;
  width: 100%;
  max-width: 800px;
  padding: 0 10px;
  margin: 0 auto;
}

.header {
  background-color: var(--header-bg-color);
  color: var(--header-color);
  transition-duration: var(--transition-duration);
  transition-timing-function: var(--transition-timing-function);
  transition-property: background-color, color;
  padding: 10px 0;
}

.main {
  flex: 1;
  padding: 40px 0;
  color: var(--main-color);
  background-color: var(--main-bg-color);
  transition-duration: var(--transition-duration);
  transition-timing-function: var(--transition-timing-function);
  transition-property: background-color, color;
}

.footer {
  padding: 40px 0;
  background-color: var(--footer-bg-color);
  color: var(--footer-color);
  transition-duration: var(--transition-duration);
  transition-timing-function: var(--transition-timing-function);
  transition-property: background-color, color;
}

.header__container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header__logo {
  --width: 50px;
  min-width: var(--width);
  max-width: var(--width);
}

.form {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 5px;
}

.input-group-radio {
  cursor: pointer;
}

.input-group-radio input[type=radio],
.input-group-radio label {
  cursor: inherit;
}

.input-group-radio label {
  padding-left: 5px;
}

.input-group {
  display: flex;
  align-items: center;
  user-select: none;
}

.as-console-wrapper, .as-console {
  height: 0;
  overflow: hidden !important;
  border: none !important;
}
<header class="header">
  <div class="container">
    <div class="header__container">
      <div class="header__logo">
        <img src="https://picsum.photos/50" alt="">
      </div>
      <form class="checkbox-theme form" id="checkbox-theme">

        <div class="input-group input-group-radio">
          <input type="radio" id="default-theme" class="radio radio-theme" name="theme" value="default-theme" checked>
          <label for="default-theme">По умолчанию</label>
        </div>

        <div class="input-group input-group-radio">
          <input type="radio" id="light-theme" class="radio radio-theme" name="theme" value="light-theme">
          <label for="light-theme">Светлая тема</label>
        </div>

        <div class="input-group input-group-radio">
          <input type="radio" id="dark-theme" class="radio radio-theme" name="theme" value="dark-theme">
          <label for="dark-theme">Тёмная тема</label>
        </div>

      </form>
    </div>
  </div>
</header>

<main class="main">
  <div class="container">
    <p>
      Lorem ipsum dolor sit amet consectetur, adipisicing elit. Rerum unde quas quo ratione accusamus enim voluptatem beatae, cupiditate maiores facilis voluptatum eveniet neque doloremque dicta aperiam corporis vel illo accusantium.
    </p>

    <p>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Soluta, nisi perspiciatis labore sed ad alias porro magnam recusandae facere amet similique, repudiandae beatae minima quia inventore, rerum praesentium totam expedita?
    </p>

    <p>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Soluta, nisi perspiciatis labore sed ad alias porro magnam recusandae facere amet similique, repudiandae beatae minima quia inventore, rerum praesentium totam expedita?
    </p>
  </div>
</main>

<footer class="footer">
  <div class="container">
    <div class="footer__container">
      Это футер
    </div>
  </div>
</footer>

javascript
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. user176262
    2022-01-31T22:42:04Z2022-01-31T22:42:04Z
      choosingDesiredTheme(theme) {
        const matchMediaDarkListener = (event) => {
          const darkActive = event.matches;
          if (darkActive) {
            this.addTheme('dark-theme');
          } else {
            this.addTheme('light-theme');
          } 
        }
    
        if (theme !== 'default-theme') {
          this.matchMediaDark.removeEventListener('change', matchMediaDarkListener);
          this.addTheme(theme);
        } else if (theme === 'default-theme') {
          const themeDefault = this.getDefaultTheme();
          this.matchMediaDark.addEventListener('change', matchMediaDarkListener);
          this.addTheme(themeDefault);
        }
      }
    
    • 1
  2. Best Answer
    Grundy
    2022-01-31T23:01:30Z2022-01-31T23:01:30Z

    对于不显示在控制台输出上的对象字段,该字段不得出现在对象中。

    因此,您可以使用闭包来解决问题。

    将函数保存在闭包中并在类中使用它就足够了,例如:

    const ThemeRadio = function(){
        function matchMediaDarkListener (event) { ... }
        return class ThemeRadio {
            ...
    
            choosingDesiredTheme(theme) {
                if (theme !== 'default-theme') {
                  this.matchMediaDark.removeEventListener('change', matchMediaDarkListener);
                  this.addTheme(theme);
                } else if (theme === 'default-theme') {
                  const themeDefault = this.getDefaultTheme();
                  this.matchMediaDark.addEventListener('change', matchMediaDarkListener);
                  this.addTheme(themeDefault);
                }
            }
    
            ...
        }
    }();
    
    • 1

相关问题

  • 第二个 Instagram 按钮的 CSS 属性

  • 由于模糊,内容不可见

  • 弹出队列。消息显示不正确

  • 是否可以在 for 循环中插入提示?

  • 如何将 JSON 请求中的信息输出到数据表 Vuetify vue.js?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5