type A = Record<'a' | 'b' | 'c', string>
const c: A = {a: "a", b: "b", c: "c"};
class类的属性名
class A {
a: number = 1;
b: string = "";
}
type AFields = keyof InstanceType; // "a" | "b"
反转
type A = {
a: "x";
b: "y";
}
type Reverse> = {
[P in T[keyof T]]: {
[K in keyof T]: T[K] extends P ? K : never
}[keyof T]
}
type B = Reverse;
const b: B = {x: "a", y: "b"};
传入不同的值返回的结果类型不同
interface A {
a: string;
}
interface B {
b: string;
}
interface C {
c: string;
}
interface Types {
a: A;
b: B;
c: C;
}
function fun (type: T): Types[T] {
return ;
}
功能
全屏
// 进入全屏
function enter(ele) {
if (ele.requestFullScreen) {
ele.requestFullScreen();
} else if (ele.mozRequestFullScreen) {
ele.mozRequestFullScreen();
} else if (ele.webkitRequestFullScreen) {
ele.webkitRequestFullScreen();
} else if (ele.msRequestFullScreen) {
ele.msRequestFullScreen();
}
}
// 退出全屏
function exit() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
}
// 是否全屏
function isFullscreen() {
return Boolean(
document.fullscreenElement ||
document.mozFullscreenElement ||
document.webkitFullscreenElement ||
document.msFullscreenElement ||
null
);
}
let composition = false;
input.addEventListener("input", function () {
if (!composition) {
search();
}
});
// 开始输入中文拼音了
input.addEventListener("compositionstart", function () {
composition = true;
});
// 结束输入中文拼音了
input.addEventListener("compositionend", function () {
composition = false;
search();
});
公共函数
类型判断
function determineType(target) {
const type = typeof target;
switch (type) {
case "bigint":
case "boolean":
case "function":
case "number":
case "string":
case "symbol":
case "undefined":
return type;
case "object":
if (target instanceof Array) {
return 'array';
}
if (target instanceof Object) {
return 'object';
}
return 'null';
default:
return "undefined";
}
}
数组分组
function groupBy (arr, generateKey) {
if (typeof generateKey === "string") {
const propName = generateKey;
generateKey = (item) => item[propName];
}
const result = {};
for (let i = 0; i < arr.length; i++) {
const key = generateKey(arr[i], i, arr);
if (result[key]) {
result[key].push(arr[i]);
} else {
result[key] = [arr[i]];
}
}
return result;
}
public class OcuppyMoreThanHalf {
/**
* Q74 数组中有一个数字出现的次数超过了数组长度的一半,找出这个数字
* two solutions:
* 1.O(n)
* see <beauty of coding>--每次删除两个不同的数字,不改变数组的特性
* 2.O(nlogn)
* 排序。中间
cygwin很多命令显示command not found的解决办法
修改cygwin.BAT文件如下
@echo off
D:
set CYGWIN=tty notitle glob
set PATH=%PATH%;d:\cygwin\bin;d:\cygwin\sbin;d:\cygwin\usr\bin;d:\cygwin\usr\sbin;d:\cygwin\us