Skip to content

Instantly share code, notes, and snippets.

View lilywang711's full-sized avatar
🎯
working

lily wang lilywang711

🎯
working
View GitHub Profile
// Import stylesheets
import './style.css';
// Write Javascript code!
const appDiv = document.getElementById('app');
appDiv.innerHTML = `<h1>Linked list traversal</h1>`;
function log(value) {
const span = document.createElement('span');
span.textContent = value + ', ';
class LinkedListNode {
constructor(value) {
this.value = value;
this.next = null;
}
}
const head = Symbol("linked list's head");
class LinkedList {
@lilywang711
lilywang711 / queryAllSelectedAPI.js
Last active November 4, 2020 10:17
生成 NEI 上所有已选择接口的交付格式
function queryAllSelectedAPI() {
const selectedList = document.querySelectorAll('.list-bd .list-group-wrap .list-row.js-selected');
return Array.from({ length: selectedList.length }).map((_, index) => {
const node = selectedList[index]
const title = node.querySelector('div.list-col.col-name > a').innerText
const link = node.querySelector('div.list-col.col-name > a').getAttribute('href')
const name = node.querySelector('div.list-col.col-creator-realname').innerText
return `${index+1}、 ${title}(${name}): ${location.origin}${link}`
}).join('\n')
var type = "window";
const obj = {
type: "obj",
foo(a, b) {
console.log("this.type", this.type, a, b);
}
};
Function.prototype.customCall = function (context, ...restArgs) {
Array.prototype.customReduce = function (fn, initialValue) {
for (let i = 0; i < this.length; i++) {
// 当未传初始值时取数组中的第一项作为初始值
if (typeof initialValue === "undefined") {
initialValue = fn(this[i], this[i + 1], i, this);
i++;
} else {
initialValue = fn(initialValue, this[i], i, this);
}
}
const compose = (...fns) => (input) => fns.reverse().reduce((acc, fn) => fn(acc), input);
const addOne = (x) => x + 1;
const square = (x) => x * x;
console.log(compose(addOne, square)(2));
const pipe = (...fns) => (input) => fns.reduce((acc, fn) => fn(acc), input);
const addOne = (x) => x + 1;
const square = (x) => x * x;
console.log(pipe(addOne, square)(2));
@lilywang711
lilywang711 / useImperativeHandle.tsx
Created July 30, 2020 06:47
useImperativeHandle in typescript
// 定义
export interface MyInputHandles {
focus(): void;
}
const MyInput: RefForwardingComponent<MyInputHandles, MyInputProps> = (
props,
ref
) => {
const inputRef = useRef<HTMLInputElement>(null);
@lilywang711
lilywang711 / umpromisify.ts
Last active July 30, 2020 06:11
反解 promise 类型
type PromiseType<T> = (args: any[]) => Promise<T>;
type UnPromisify<T> = T extends PromiseType<infer U> ? U : never;
// 或者这样一句话: type UnPromisify<T> = T extends (args: any[]) => Promise<infer U> ? U : never;
// 测试 UnPromisify<T>:
async function stringPromise() {
return "string promise";
}
async function numberPromise() {
@lilywang711
lilywang711 / override-settimeout.js
Last active July 13, 2020 04:06
重写 window.setTimeout ,收集所有的 timerId,清除页面上所有定时器
function overrideSetTimeout(fn) {
let timerIds = [];
function realSetTimeout(callback, delay) {
const args = Array.prototype.slice.call(arguments, 2)
const timerId = fn(() => {
callback.apply(null, args)
}, delay)
timerIds.push(timerId);