Git Product home page Git Product logo

blog's People

Contributors

lkdghzh avatar lkgo avatar zhuuuu avatar

Stargazers

 avatar  avatar

blog's Issues

algorithm:最短编辑距离

// dynamic program way
// https://blog.csdn.net/a553181867/article/details/89008264
/**
 * @param {string} word1
 * @param {string} word2
 * @return {number}
 */
var minDistanceTest = function (word1, word2) {
    console.log(word1, word2);
    const distanceTable = Array.from({ length: word1.length + 1 }, (_, i) => {
        const fisrtColumn = Array.from({ length: word2.length + 1 }, (_, j) => j)
        const column = new Array(word2.length + 1).fill(i, 0, 1).fill(undefined, 1);
        return i === 0 ? fisrtColumn : column
    });
    for (var i = 1; i <= word1.length; i++) {
        for (var j = 1; j <= word2.length; j++) {
            // dp 
            var flag = 1;
            if (word1.charAt(i - 1) === word2.charAt(j - 1)) {
                flag = 0
            }
            const left = distanceTable[i - 1][j] + 1;
            const top = distanceTable[i][j - 1] + 1;
            const leftTop = distanceTable[i - 1][j - 1] + flag;

            console.log(`${i},${j}`, Math.min(left, Math.min(top, leftTop)))
            distanceTable[i][j] = Math.min(left, Math.min(top, leftTop))
        }
    }
    console.log('distanceTable', distanceTable)
    console.log('distanceTable[i][j]', distanceTable[word1.length][word2.length])
    return distanceTable[word1.length][word2.length]
};
minDistanceTest('horse', 'ros')

this

const o = {
  fn: () => {
    console.log(this)
  },
  fn1() {
    console.log(this)
  },
  fn2() {

    const fn2a = () => {
      console.log(this)
    }
    return fn2a()
  }
}
o.fn()
o.fn1()
o.fn2()
var f = o.fn2;
f()
var o2 = { f: o.fn2 }
o2.f()


// https://juejin.cn/post/7035257186565488670
// https://www.bilibili.com/video/BV1qD4y1G7YK/?spm_id_from=333.788&vd_source=6295663755b5924d0280f874eacbe51d
// 17min

Refect 唯手熟尔

Refect

将Object对象的一些明显属于语言内部的方法(比如Object.defineProperty),放到Reflect对象上。现阶段,某些方法同时在Object和Reflect对象上部署,未来的新方法将只部署在Reflect对象上。也就是说,从Reflect对象上可以拿到语言内部的方法

可以通过getOwnPropertyDescriptor拿到属性accessor,执行(可以改变上下文)

var o1 = {
  x: 1,
  get a() {
    return this.x
  }
}
console.log(Reflect.getOwnPropertyDescriptor(o1, 'a'))
/*
{
    "enumerable": true,
    "configurable": true,
     get: ƒ a(),
     set: undefined
}
*/
var _get = Reflect.getOwnPropertyDescriptor(o1, 'a').get;

var o2={
  x:2
}
_get.call(o2) //2

class

1.类属性方法和原型方法定义

  • a是类构造方法上的属性,
  • f是类构造方法上的方法
    • 一般情况下方法是定义在类原型上。不过这种定义方式在实际开发中也会看到。
    • 但是要知道这种方法是属于构造函数,每次实例化A对象的时候都会在每个创建个f函数
  • 在类内部fn(){}等价A.prototype.fn=()=>{}
class A {
  a = 1;
  constructor({ a }) {
    this.a = a
  };
  f = () => { };
  fn() {
    console.log(1)
  }
}
A.prototype.fn()

2.super的理解

1.子类如果显示写constructor,必须手动调用super

class A { }
class B extends A {
  constructor() { }
}
new B()

// Uncaught ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
class A { }
class B extends A {
  constructor() {
    super()
  }
}
new B()
// https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Classes/constructor

设计模式-实现-备忘录-memento

// 一条记忆实体(内容)
// 一条备忘
// 仅仅描述一条实体的类。【用value通用,各种数据都能备忘]
class Memento {
    constructor(value) {
        this.value = value;
    }
}
// 多条备忘的集合
// 提供([仅从顶部新增、删除])添加备忘 撤销备忘 ,无删除
class MementosStorage {
    constructor(memento) {
        // TODO Memento 类型校验
        this.value = [memento]
    }
    get length() {
        return this.value.length
    }
    // do 
    add(memento) {
        return this.value.push(memento)
    }
    // --> undo
    remove(memento) {
        return this.value.pop(memento)
    }
}

class MementosManager {
    constructor(memento) {
        this.storage = this.value = new MementosStorage(memento)
    }
    addMemento(memento) {
        return this.storage.add(memento);
    }
    undo() {
        return this.storage.remove();
    }
    getMemento(index = this.storage.length - 1) {
        return this.storage.value[index]
    }
    motify(index = this.storage.length - 1, memento) {
        this.storage.value[index] = memento
    }
}

const li = {
    name: "li",
    phone: 15120065082
}
const zhang={
    name: "zhang",
    phone: 1
}
const wang={
    name: "wang",
    phone: 2
}
const zhao ={
    name: "zhao",
    phone: 3
}
const manager = new MementosManager(li);
var item = manager.getMemento()
console.log('item init', item)  // {name: "li", phone: 15120065082}

manager.addMemento(zhang)
var item = manager.getMemento()
console.log('item after add', item) // {name: "zhang", phone: 1}

manager.addMemento(wang)
var item = manager.getMemento()
console.log('item after add', item) // {name: "wang",phone: 2}

manager.addMemento(zhao)
var item = manager.getMemento()
console.log('item after add', item) // {name: "zhao", phone: 3}

manager.undo()
var item = manager.getMemento()
console.log('item after undo',item); // {name: "wang", phone: 2}

manager.undo()
var item = manager.getMemento()
console.log('item after undo',item); // {name: "zhang", phone: 1}

manager.undo()
var item = manager.getMemento()
console.log('item after undo',item); // {name: "li", phone: 15120065082}

c++

char wchar_t string wstring
char
wchar_t 1 1
string 1
wstring 1 1

side effect

什么是side effect

运用在函数中,中文(边际效应,附作用),除了函数本身,还有一些附加左右
例如:

express源码分析

const express =require('../lib/express');
const app=express()
app.get('/',function(req,res,next){
  console.log(1)
  next()
},function(req,res,next){
  console.log(2)
  next()
},function(req,res){
  res.end('end')
})
app.listen(3000)


/**
 *
 *  app.use()           //中间件的使用
 *  app.get()           //多个路由层级中间件和回调
 *  app.route()         //restFul
 *  express.Router()    //二级路由
 */

express

按照两个阶段去学习

1.启动阶段(框架根据手写app router midsware初始化并启动)
2.请求阶段(get post put delete )require('http').METHODS

启动阶段

路由的核心部分

method
path
handler

具体方法

express是一个函数(一个闭包,返回一个函数) app {}        Router 是一个函数(一个闭包,返回一个函数,可以new ,可以router)stack   Route (class)几个path new几个route stack
Router                                 method
Route                                  use
                                       route
                                       param
                                       handle ...

调用关系

app express返回的函数       Router(闭包的好处之一,匿名函数返回的函数) Route    Layer

app.method(path,handler)
   - app.lazyrouter()     - new Router()得到router实例(是一个函数)
   - router.route(path)   - new Route(path)得到route实例
                          - router.route = route实例
   - route.method
   - return app

app.use
app.route
app.param

路由图

React高阶组件

函数

装饰器

代理高阶

继承高阶

高阶组件开发使用场景

debounce

const start1 = Date.now()

function debounce(fn, delay = 5000) {
  let timer = null;
  // 闭包
  return function () {
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      timer = null
      fn()
    }, delay)
  }
}

const f = () => { console.log('执行了当前函数', Date.now() - start1) }

const df = debounce(f, 5000)

//模拟每次点击
const clk = _ => new Promise((resolve) => {
  setTimeout(() => {
    df()
    console.log('clk')
    resolve()
  }, 1000)
});

// 每次点击间隔1000ms,对应的闭包还没放到宏队列
// 在1000ms内,下次点击会立刻清掉上次点击的定时器,四次点击造成前三次都被清除了
// 直到第四次执行,在

const start = Date.now()
console.log('开始')
//第一次点击
console.log('clk')
df();

// 开始
// clk
// 执行了当前函数 5004


// (async function () {
//   console.log('第一次点击耗时', Date.now() - start);
//   //第一次点击
//   await clk();
//   console.log('第二次点击耗时', Date.now() - start)
//   //第二次点击
//   await clk();
//   console.log('第三次点击耗时', Date.now() - start)
//   //第四次点击
//   await clk();
//   console.log('第四次点击耗时', Date.now() - start)
// })()
// 开始
// clk
// 第一次点击耗时 4
// clk
// 第二次点击耗时 1008
// clk
// 第三次点击耗时 2010
// clk
// 第四次点击耗时 3012
// 执行了当前函数 8011


// 优点:防抖可以防止多次抖动执行一次
// 弊端:最后一次延迟delay执行

Issue的使用意见

认可

  • 可以尝试使用issue写文章
  • 可以尝试使用issue写wiki
  • 可以尝试使用issue存图片

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.