我想通过display属性的变化来检测CSS 属性的值变化class,我想出了下面的代码片段。
我知道getComputedStyle返回一个只读的活动CSSStyleDeclaration对象,但是当元素的样式改变时对象会自动更新,我假设它以某种方式分配了它的属性。
但它没有调用 getter 和 setter。为什么会发生这种情况,然后它在只读时如何分配其属性?
let parent = document.querySelector(".parent");
let child = parent.querySelector(".child");
let style = getComputedStyle(child);
let display = Symbol("display");
style[display] = style.display;
Object.defineProperty(style, "display", {
get() {
console.log("getter");
return style[display];
},
set(value) {
console.log("setter", value);
style[display] = value;
}
});
let button = document.querySelector("button");
button.addEventListener("click", () => {
child.classList.toggle("hide");
});Run Code Online (Sandbox Code Playgroud)
.child {
height: 100px;
background-color: #80a0c0;
}
.hide {
display: none;
}Run Code Online (Sandbox Code Playgroud)
<button>Toggle</button>
<div class="parent">
<div …Run Code Online (Sandbox Code Playgroud)我的 sass 模块可以像下面这样相互导入。
// LinearLayout.scss
@mixin LinearLayout { ... }
linear-layout { @include LinearLayout; }
// ScrollView.scss
@use "LinearLayout" as *;
@mixin ScrollView {
@include LinearLayout;
...
}
scroll-view { @include ScrollView; }
Run Code Online (Sandbox Code Playgroud)
并且由于每个sass 模块都通过在脚本中导入而最终出现在捆绑的 css 文件中,因此捆绑的 css 文件包含重复的 css 选择器。
如何删除重复项?
TypeScript 中的“keyof typeof”是什么意思?
我发现了上面的问题,作者问了我想问的完全相同的问题,但没有答案找到问题的重点。
假设有这个通用方法和一个类。
function fn<K extends keyof typeof A>(key: K) ...
class A {
static a;
static b;
}
Run Code Online (Sandbox Code Playgroud)
我的理由是:
typeof 关键字返回一个字符串,显示 js 基本类型之一。
所以无论值是什么,返回类型都是字符串。
所以 keyof "..." 应该是索引和“长度”以及字符串文字可以拥有的东西。在这种情况下,keyof "function" 应该返回类型 "0" | "1" | "2" | "3" | "4" | “5” | "6" | "7" | “长度”。
但实际行为是 typeof 类 A 返回构造函数。
这怎么会发生?