本文实例讲述了原生javascript单例模式的应用。分享给大家供大家参考,具体如下:
总体原则:开闭原则(Open Close Principle) 开闭原则就是说对扩展开放,对修改关闭。在程序需要进行扩展的时候,不能去修改原有 的代码,实现一个热插拔的效果。所以一句话概括就是:为了使程序的扩展性好,易于维护和升 级。 1、单一职责原则 不要存在多于一个导致类变更的原因,也就是说每个类应该实现单一的职责,如若不然,就 应该把类拆分。
创建模式_单例模式 "htmlcode">
<script>
let singleton = function (){
function Map(width,height) {
this.width = width;
this.height = height;
}
let instance;
return{
getIntence : function(width,height){
if(instance == undefined){
instance = new Map(width,height);
}else{
instance.width = width;
instance.height = height;
}
return instance;
}
}
}();
let m1 = singleton.getIntence(100,200);
let m2 = singleton.getIntence(200,300);
console.log(m1);//200 300
console.log(m2);//200 300
</script>
飞机大战中单例模式的应用
地图部分
let mapSingleton = (function(){
function Map(width,height,background){
this.domObj = null;//地图的div
this.moveBox = null;
this.width = width;
this.height = height;
this.background = background;
this.enemyPlanes = [];//敌机数组
this.myPlanes = [];//我方战机数组
this.createUI();
this.moveBg();
}
//853 600
Map.prototype.createUI = function() {
//1、地图的div
this.domObj = document.createElement("div");
this.domObj.style.cssText = `margin:20px auto;position: relative;width:${this.width}px;height:${this.height}px;overflow:hidden`;
document.body.appendChild(this.domObj);
//2、移动的div
this.moveBox = document.createElement("div");
this.moveBox.style.cssText = `position: absolute;
top:-1106px;
width: 480px;
height: 1706px;`;
this.domObj.appendChild(this.moveBox);
//3、图片
for(var i=0;i<2;i++){
let img01 = document.createElement("img");
img01.src = this.background;
img01.style.cssText = `display: block`;
this.moveBox.appendChild(img01);
}
//4、积分板:
this.scoreDom = document.createElement("div");
this.scoreDom.style.cssText = "position:absolute;left:0px;top:0px;width:100px;height:35px;z-index:999";
this.scoreDom.innerHTML = 0;
this.domObj.appendChild(this.scoreDom);
};
Map.prototype.moveBg = function(){
let top1 = -1106;
setInterval(()=>{
top1++;
if(top1>=-253){
top1 = -1106;
}
this.moveBox.style.top = top1+"px";
},50);
}
var instance;
return {
getInstance:function(width,height,background){
if(instance==undefined){
instance = new Map(width,height,background);
}else{
instance.width = width;
instance.height = height;
instance.background = background;
instance.domObj.style.width=this.width+"px";
instance.domObj.style.height=this.height+"px";
instance.moveBox.children[0].src=this.background;
instance.moveBox.children[1].src=this.background;
}
return instance;
}
}
})();
// 单例模式的总结
/*采用闭包原理和自调用原理,先进行自调用噶、返回闭包函数,让使用者调用,但只能创建一个实例对象,当要创建多个的时候 不会进行创建,最上面简单案例代码,如果要再次创建实例,会覆盖之前的数据,并且不会创建实例对象
为什么要采用闭包 防止使用者再次构造新对象 采用自调用之后 函数执行1次 在调用时 只需要使用return的函数就行 避免用户多次构造新对象*/
感兴趣的朋友可以使用在线HTML/CSS/JavaScript前端代码调试运行工具:http://tools.jb51.net/code/WebCodeRun测试上述代码运行效果。
更多关于JavaScript相关内容还可查看本站专题:《javascript面向对象入门教程》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。
标签:
javascript,单例模式
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“原生javascript单例模式的应用实例分析”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新动态
2025年11月01日
2025年11月01日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]