【青训营】JavaScript(1)

653 阅读1分钟

写好JS的一些原则

各司其职

实现浅深两种主题

方案一

存在的问题

方案二

code.h5jun.com/fapaz/edit?…

方案三

组件封装

方案一

结构化设计

code.h5jun.com/tifuf/edit?…

展现效果: CSS

行为设计: API

控制流

code.h5jun.com/reba/edit?c…

  • 使用自定义事件来解耦

方案二

解耦

  • 将控制元素抽取成插件
  • 插件与组件之间通过依赖注入方式建立联系

code.h5jun.com/weru/edit?c…

只解耦了一半

方案三

code.h5jun.com/vata/edit?c…

过程抽象

  • 高阶函数

  • once设计模式

code.h5jun.com/gale/edit?c…

code.h5jun.com/wik/edit?cs…

code.h5jun.com/roka/edit?h…

code.h5jun.com/bucu/edit?h…

code.h5jun.com/kapef/edit?…

  • 纯函数(输入唯一 输出唯一 )
  • 非纯函数

code.h5jun.com/mohi/edit?h…

let x=10;
function foo() {
   return x++; 
}
function bar() {
    return x*2
}
foo();
foo();

function reducible(fn) {
 return function(...args) {
   return args.reduce(fn);
 } 
}

const add = reducible((x, y) => {
  return
})
// lib.js

function foo() {
    
}

命令式与声明式

  • 命令式
    • pascal
    • C++
    • JAVA
  • 声明式

code.h5jun.com/tuda/edit?h…

code.h5jun.com/nal/edit?ht…