前言
我是歌谣 放弃很容易 但是坚持一定很酷 微信公众号关注前端小歌谣带你进入前端巅峰交流群 今天继续对前端知识的小结
命令模式宏命令
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <title>命令模式宏命令</title> |
| </head> |
| <body> |
| <script> |
| var command1 = { |
| execute: function(){ |
| console.log(1); |
| } |
| }; |
| var command2 = { |
| execute: function(){ |
| console.log(2); |
| } |
| }; |
| var command3 = { |
| execute: function(){ |
| console.log(3); |
| } |
| }; |
| |
| |
| |
| var command = function(){ |
| return { |
| commandsList: [], |
| add: function(command){ |
| this.commandsList.push(command); |
| }, |
| execute: function(){ |
| for(var i = 0,commands = this.commandsList.length; i < commands; i+=1) { |
| this.commandsList[i].execute(); |
| } |
| } |
| } |
| }; |
| |
| var c = command(); |
| c.add(command1); |
| c.add(command2); |
| c.add(command3); |
| c.execute(); |
| </script> |
| </body> |
| </html> |
命令模式的例子
| <!DOCTYPE html> |
| <html lang="en"> |
| |
| <head> |
| <meta charset="UTF-8"> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <title>命令模式</title> |
| </head> |
| |
| <body> |
| <button id="button1">刷新菜单目录</button> |
| <button id="button2">增加子菜单</button> |
| <button id="button3">删除子菜单</button> |
| <script> |
| var b1 = document.getElementById("button1"), |
| b2 = document.getElementById("button2"), |
| b3 = document.getElementById("button3"); |
| |
| |
| var setCommand = function (button, command) { |
| button.onclick = function () { |
| command.execute(); |
| } |
| }; |
| |
| var MenuBar = { |
| refersh: function () { |
| alert("刷新菜单目录"); |
| } |
| }; |
| var SubMenu = { |
| add: function () { |
| alert("增加子菜单"); |
| }, |
| del: function () { |
| alert("删除子菜单"); |
| } |
| }; |
| |
| var RefreshMenuBarCommand = function (receiver) { |
| this.receiver = receiver; |
| }; |
| RefreshMenuBarCommand.prototype.execute = function () { |
| this.receiver.refersh(); |
| } |
| |
| var AddSubMenuCommand = function (receiver) { |
| this.receiver = receiver; |
| }; |
| AddSubMenuCommand.prototype.execute = function () { |
| this.receiver.add(); |
| } |
| |
| var DelSubMenuCommand = function (receiver) { |
| this.receiver = receiver; |
| }; |
| DelSubMenuCommand.prototype.execute = function () { |
| this.receiver.del(); |
| } |
| |
| var refershBtn = new RefreshMenuBarCommand(MenuBar); |
| var addBtn = new AddSubMenuCommand(SubMenu); |
| var delBtn = new DelSubMenuCommand(SubMenu); |
| |
| setCommand(b1, refershBtn); |
| setCommand(b2, addBtn); |
| setCommand(b3, delBtn); |
| </script> |
| </body> |
| |
| </html> |
四次挥手
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <title>四次挥手</title> |
| </head> |
| <body> |
| |
| |
| |
| |
| |
| </body> |
| </html> |
圣杯布局
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <title>圣杯布局</title> |
| </head> |
| <style> |
| |
| #container { |
| padding-left: 200px; |
| padding-right: 150px; |
| overflow: auto; |
| } |
| #container p { |
| float: left; |
| } |
| .center { |
| width: 100%; |
| background-color: lightcoral; |
| } |
| .left { |
| width: 200px; |
| position: relative; |
| left: -200px; |
| margin-left: -100%; |
| background-color: lightcyan; |
| } |
| .right { |
| width: 150px; |
| margin-right: -150px; |
| background-color: lightgreen; |
| } |
| .clearfix:after { |
| content: ""; |
| display: table; |
| clear: both; |
| } |
| </style> |
| <body> |
| |
| |
| <div id="container" class="clearfix"> |
| <p class="center">我是中间</p> |
| <p class="left">我是左边</p> |
| <p class="right">我是右边</p> |
| </div> |
| </body> |
| </html> |
在构造函数上定义方法
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8" /> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| <title>原型和原型链</title> |
| </head> |
| <body> |
| <script> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function Animai() { |
| this.name = name |
| } |
| Animai.prototype.eat = function () { |
| console.log('我爱吃饭', this.name) |
| } |
| let dog = new Animai() |
| let cat = new Animai() |
| dog.eat() |
| cat.eat() |
| console.log(dog.eat===cat.eat) |
| </script> |
| </body> |
| </html> |
垃圾清除机制
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8" /> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| <title>Document</title> |
| </head> |
| <body> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| </body> |
| </html> |
复杂工厂模式
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8" /> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| <title></title> |
| </head> |
| <body> |
| <script> |
| |
| var BicycleShop = function (name) { |
| this.name = name |
| this.method = function () { |
| return this.name |
| } |
| } |
| BicycleShop.prototype = { |
| constructor: BicycleShop, |
| |
| |
| |
| |
| sellBicycle: function (model) { |
| var bicycle = this.createBicycle(model) |
| |
| bicycle.A() |
| |
| |
| bicycle.B() |
| |
| return bicycle |
| }, |
| createBicycle: function (model) { |
| throw new Error( |
| '父类是抽象类不能直接调用,需要子类重写该方法' |
| ) |
| }, |
| } |
| |
| function extend(Sub, Sup) { |
| |
| var F = function () {} |
| |
| |
| F.prototype = Sup.prototype |
| |
| |
| Sub.prototype = new F() |
| |
| |
| Sub.prototype.constructor = Sub |
| |
| |
| Sub.sup = Sup.prototype |
| |
| if ( |
| Sup.prototype.constructor === Object.prototype.constructor |
| ) { |
| |
| Sup.prototype.constructor = Sup |
| } |
| } |
| var BicycleChild = function (name) { |
| this.name = name |
| |
| BicycleShop.call(this, name) |
| } |
| |
| extend(BicycleChild, BicycleShop) |
| |
| BicycleChild.prototype.createBicycle = function () { |
| var A = function () { |
| console.log('执行A业务操作') |
| } |
| var B = function () { |
| console.log('执行B业务操作') |
| } |
| return { |
| A: A, |
| B: B, |
| } |
| } |
| var childClass = new BicycleChild('歌谣') |
| console.log(childClass,"childClass") |
| console.log(childClass.createBicycle()) |
| console.log(childClass.sellBicycle()) |
| </script> |
| </body> |
| </html> |
如何写一个简单的回调函数
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <title>如何写一个简单的回调函数</title> |
| </head> |
| <body> |
| <script> |
| |
| |
| function fangfang(callback) { |
| var data = { |
| "name": "fangyalan", |
| "age": 18 |
| }; |
| callback(data); |
| } |
| |
| fangfang(function (data) { |
| |
| console.log(data) |
| }) |
| |
| |
| </script> |
| </body> |
| </html> |
总结
我是歌谣 最好的种树是十年前 其次是现在 加油 歌谣
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!