<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="show">
hello world
</div>
</body>
</html>
<script>
//单例模式用来定义命名空间
let Yqf = {
g:function (id) {
return document.getElementById(id);
},
css:function (id,key,value) {
this.g(id).style[key] = value;
console.log(value);
}
}
Yqf.css("show","display","block");
</script>
<script>
//创建一个属于自己的代码库
let Yqf = {
Util:{
util1_method:function () {
console.log("i am util1 method");
},
util2_method:function(){
console.log("i am util method");
}
},
Tool:{
tool_method:function () {
console.log("i am Tool1 methond");
},
tool2_method:function () {
console.log('i am tool2 methond')
}
},
}
Yqf.Util.util1_method();
Yqf.Tool.tool2_method();
</script>
<script>
//无法修改的静态变量
let Confing = (function () {
let config ={
Max:100,
Min:10,
count:100
}
return {
get:function (name) {
return config[name]?config[name]:"没有"+name+"相关属性";
}
}
})();
console.log(Confing.get("Max"));
</script>
<script>
//惰性创建单例--延迟创建单例
let Lazysingle = (function () {
let instance = false;
let Single = function () {
return {
publicsingle:function () {
},
a:"hello world"
}
}
if(!instance){
return Single();
}
})();
console.log(Lazysingle.a);
</script>
js 面向对象设计模式--单例设计模式
最新推荐文章于 2020-04-28 10:42:44 发布