标签:text 属性 weight ase length 衣服 一个 函数 文献
const Model = function(sex) { this.sex = sex; } Model.prototype.takePhoto = function() { console.log(‘sex= ‘ + this.sex + ‘underwear‘ + this.underwear); } const maleModel = new Model(‘male‘); const femaleModel = new Model(‘female‘); for(let i = 1; i <= 50; i++) { maleModel.underwear = ‘underwear‘ + i; femaleModel.takePhoto(); } for(let j = 1; j <= 50; j++) { femaleModel.underwear = ‘underwear‘ + j; femaleModel.takePhoto(); }
let id = 0; window.startUpload = function(uploadType, files) { for(let i = 0, file; file = files[i++];) { const uploadObj = new Upload(uploadType, file.fileName, file.fileSize); uploadObj.init(id++); } } const Upload = function(uploadType, fileName, fileSize) { this.uploadType = uploadType; this.fileName = fileName; this.fileSize = fileSize; this.dom = null; } Upload.prototype.init = function(id) { this.id = id; this.dom = document.createElement(‘div‘); ...创建上传成功的fileName和fileSize相关展示DOM、绑定删除事件delFile()、插入DOM } Upload.prototype.delFile = function() { ...删除DOM节点及对象 }
调用
startUpload(‘plugin‘, [ { fileName: ‘1.txt‘, fileSize: 1000, }, { fileName: ‘2.txt‘, fileSize: 3000, }, { fileName: ‘3.txt‘, fileSize: 5000, }, ]);
const Upload = function(uploadType) { this.uploadType = uploadType; } Upload.prototype.delFile = function(id) { uploadManager.setExternalState(id, this); ...删除DOM } const UploadFactory = (function() { const createdFlyWeightObjs = {}; return { create(uploadType) { if (createdFlyWeightObjs[uploadType]) { return createdFlyWeightObjs[uploadType]; } return createdFlyWeightObjs[uploadType] = new Upload(uploadType); } } })(); const uploadManager = (function() { const uploadDatabase = {}; return { add(id, uploadType, fileName, fileSize) { // 仅仅创建基本type const flyWeightObj = UploadFactory.create(uploadType); ...添加展示的DOM和相应的删除事件delFile() // 存放额外的属性,通过调用 setExternalState() 函数将以下属性赋值到flyWeightObj上 uploadDatabase[id] = { fileName, fileSize, dom, }; return flyWeightObj; }, setExternalState(id, flyWeightObj) { const uploadData = uploadDatabase[id]; for(const i in uploadData) { flyWeightObj[i] = uploadData[i]; } }, } })(); // 开始出发上传动作的startUpload函数 let id = 0; window.startUpload = function(uploadType, files) { for(let i = 0, file; file = files[i++];) { const uploadObj = uploadManager.add(++id, uploadType, file.fileName, file.fileSize); } } // 使用 startUpload(‘plugin‘, [ { fileName: ‘1.txt‘, fileSize: 1000, }, { fileName: ‘2.txt‘, fileSize: 3000, }, { fileName: ‘3.txt‘, fileSize: 5000, }, ]);
const objectPoolFactory = function(createObjFn) { const objectPool = []; return { create() { const obj === objectPool.length ? createObjFn.apply(this, arguments) : objectPool.shift(); return obj; } recover(obj) { objectPool.push(obj); } } } const iframeFactory = objectPoolFactory(function() { const iframe = document.createElement(‘iframe‘); document.body.appendChild(iframe); iframe.onload = function() { iframe.onload = null; iframeFactory.recover(iframe); } return iframe; }); const iframe1 = iframeFactory.create(); iframe1.src = ‘http://baidu.com‘; const iframe2 = iframeFactory.create(); iframe2.src = ‘http://QQ.com‘;
参考文献:
[1] 《JavaScript设计模式与开发时间》,曾探,中国工信出版集团.
标签:text 属性 weight ase length 衣服 一个 函数 文献
原文地址:https://www.cnblogs.com/yijingzheng/p/10367101.html