function solve(n, V, vw){
let dp = new Array(n +1).fill(0).map(()=> new Array(V).fill(0))for(let i =1; i <= n; i++){for(let j =1; j <= V; j++){if(j < vw[i -1][0]){
dp[i][j]= dp[i -1][j]}else{
dp[i][j]= Math.max(dp[i -1][j], dp[i -1][j - vw[i -1][0]]+ vw[i -1][1])}}}return dp[n][V]}
手写简易发布订阅模式
class EventHub {
map ={}on(name, fn){
this.map[name]= this.map[name]||[]
this.map[name].push(fn)}off(name, fn){
this.map[name]= this.map[name]||[]
let index = this.map[name].indexOf(fn)if(index <0)return
this.map[name].splice(index,1)}emit(name, data){
this.map[name]= this.map[name]||[]for(let event of this.map[name]){
event.call(this, data)}}}const e = new EventHub()const func1 =function(name){
console.log('hello'+ name)}const func2 =function(name){
console.log('bye'+ name)}
e.on('click', func1)
e.on('click', func2)
e.off('click', func1)
e.emit('click','mike')
手写bind
let o ={}
function add(...args){
console.log(args.reduce((a, b)=> a + b))}
Function.prototype.mybind =function(obj,...args){
let fn = this
const fbound =function(){
let nowthis = this instanceof fbound ? this : obj
return fn.apply(nowthis, args.concat([...arguments]))}
fbound.prototype = Object.create(this.prototype)return fbound
}
add.mybind(o,1,2)(3)
let temp = add.mybind(o,10,20)
new temp(30)