
为了节省性能, 如何高效的创建对象.
让一个构造函数每次都返回同一个实例
后台登录场景 登录弹窗场景 差掉后需要重新创建嘛? 贪吃蛇的食物,需要每次都重新创建嘛?
没必要创建,就不创建.
我自己考虑的一个单例实现
1
2
3
4
5
6
7
8
9
10
11
12
13
var Test = (function () {
var abc;
return function (name) {
this.name = name;
return abc ? abc : abc = this;
}
})()
var a = new Test('one');
var b = new Test('two');
console.log(a === b);
思路是闭包,加上 如果构造函数有return 引用值 则new 会返回该引用值.
cto 第一版
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function Test (name) {
// var this = Object.create(Test.prototype)
if (Test.instance === 'object') {
return Test.instance;
}
this.name = name;
Test.instance = this;
// return this;
}
var a = new Test('one');
var b = new Test('two');
console.log(a === b);
不符合开闭原则.对扩展开放,对修改关闭.
因为在外部可修改
Test.instance = {};
第二版 闭包,惰性函数?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
function Test (name) {
var instance = this;
this.name = name;
Test = function () {
return instance;
}
}
var a = new Test('one');
var b = new Test('two');
console.log(a === b);
// 缺陷,无法完成一个继承.
//比如
Test.prototype.lastName = 'D';
console.log(a.lastName);
console.log(b.lastName);
//重写Test的时候, 两个函数的原型是不同的.
// 我想挽救了一下,
// 比如这样
function Test (name) {
var instance = Object.create(Test.prototype);
instance.name = name;
Test = function () {
return instance;
}
return Test();
}
var a = new Test('one');
Test.prototype.lastName = 'mike';
var b = new Test('two');
console.log(a === b);
console.log(a.lastName);// undefined
console.log(b.lastName);//undefined
因为instance只能继承第一个Test,而我们之后访问的都是第二个Test.prototype
想了一个很丑的办法
function Test (name) {
var instance = this;
this.name = name;
var pro = Test.prototype;
Test = function () {
// 两个思路,让这个函数的prototype 和 Test.prototype 相等
// 或者 让instance ,继承第二个prototype
for(var key in Test.prototype){// 这个地方只是这么个意思.
pro[key] = Test.prototype[key];
}
return instance;
}
}
var a = new Test('one');
Test.prototype.lastName = 'mike';
var b = new Test('two');
console.log(a === b);
console.log(a.lastName);
console.log(b.lastName);
但很明显,这种方式肯定不行..
cto 第三版 圣杯,闭包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var Test = (function () {
var instance;
return function (name) {
if (typeof instance === 'object') {
return instance
}
instance = this;
this.name = name;
}
// return this
})();
var a = new Test('one');
Test.prototype.lastName = 'mike';
var b = new Test('two');
console.log(a === b);
console.log(a.lastName);
console.log(b.lastName);
其实跟我写的基本一样, 看来我的水平还是有点提高的.
创建弹窗案例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//创建弹窗案例
//普通模式
var CreateAlert = function (text) {
var oDiv = document.createElement('div');
oDiv.style.display = 'none';
oDiv.innerText = text;
document.body.appendChild(oDiv);
return oDiv;// 这个没想到.
}
//单例模式
var CreateAlert = (function () {
var oDiv = null;
return function (text) {
if (oDiv) {
// 这里稍微加深一下小知识点, typeof null == 'object' 是true.....
return oDiv
}
oDiv = document.createElement('div');
oDiv.style.display = 'none';
oDiv.innerText = text;
document.body.appendChild(oDiv);
return oDiv;// 这个没想到.
}
})();
btn.onclick = function () {
oDiv = CreateAlert('hello duyi');
console.log(oDiv);
oDiv.style.display = 'block';
}
封装成函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function getSingle (fun) {
var instance;
return function () {
if (instance) {
return instance;
}
instance = fun.apply(this,arguments);
return instance;
}
}
//或者
function getSingle (fun) {
var instance;
return function () {
if(!instance){
instance = fun.apply(this,arguments);
}
return instance;
}
}
在这里我们就联想到了一些东西, 比如 防抖, 节流, 缓存, 这些都是利用闭包, 给原来的函数增加一个条件? 返回一个新的函数,
或者我们联想到,如果我们以前某些功能,利用了闭包, 那么我们完全可以把该闭包改成一个功能函数,(我们暂且称作函数的再次加工厂)
比如,我以前经常用锁的概念,有时,我只希望该函数只执行一次.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
btn.onclick = function () {
clog();
}
第一种,此时flag 是全局变量.
var flag;
function clog () {
if(!flag){
console.log(123);
flag = true;
}
}
第二种,此时flag在闭包中,私有变量.
var clog = (function () {
var flag;
return function clog () {
if(!flag){
console.log(123);
flag = true;
}
}
})();
之前也只到过这里.
现在发觉,所有闭包的,都可以封装成函数
第三种,
function once (fun) {
var flag;
return function () {
if(!flag){
fun.apply(this,arguments);
flag = true;
}
}
}
当然,我们也没必要把flag一定要放在 闭包中,
可以放在 函数上, 比如 clog.flag
或者如果是dom元素,则可以把flag 放在dom上.
但根据今天所学,这不符合开闭原则.
进而,有一种体会,
1
2
3
4
5
6
从结果上看,我们是让函数被调用之后,
增加了一些条件.
比如节流,防抖,缓存,单例,锁,,等等.
这些功能的实现,所需要的数据不需要从其他地方而来.
所以可以使用闭包,私有变量.
而只要能用闭包,私有变量,我们就可以让他变成一个函数工厂.
同理我们再试一把. 我们希望点击一次,切换一次
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
btn.onclick = function () {
toggleClog();
}
版本1.0
var count = 0;
function toggleClog () {
count%2 ? console.log(123) : console.log(321);
count++;
}
版本2.0 闭包
var toggleClog = (function () {
var count = 0;
return function () {
count%2 ? console.log(123) : console.log(321);
count++;
}
})();
版本3.0 封装成函数.
// 模拟toggle
function toggle (fun1,fun2) {
var count = 0;
return function () {
count%2 ? fun1() : fun2();
count++;
}
}
function fun1 () {
console.log(123);
}
function fun2 () {
console.log(321);
}
var toggleClog = toggle(fun1,fun2);
根据今天所学,我们可以认为,复杂度降低,耦合度升高了?
写到这里隐隐约约有个体验,或者推论 任何结构,都可以拆分? 像 for循环 可以封装成 forEach 像 if else 也可以封装成 类似toggle? 暂时想不明白, 我们只要知道,所有闭包的都有可能改成,”函数工厂”?