一:js单例模式

何为单例模式:单例要求一个类有且只有一个实例
es5写法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var Singleton = function(name) {
this.name = name;
this.instance = null;
}
Singleton.getInstance = function(name) {
if(!this.instance) { //这里的this指向Singleton这个类
this.instance = new Singleton(name);
}
return this.instance;
}
var a = Singleton.getInstance('sven1');//返回值为Singleton {name: "sven1", instance: null}
var b = Singleton.getInstance('sven2');//返回值为Singleton {name: "sven1", instance: null}
// 指向的是唯一实例化的对象
console.log(a === b);//true

es6写法

1
2
3
4
5
6
7
8
9
10
11
12
13
class Singleton {
constructor(name) {
this.name = name;
this.instance = null;
}
// 构造一个广为人知的接口,供用户对该类进行实例化
static getInstance(name) {
if(!this.instance) {
this.instance = new Singleton(name);
}
return this.instance;
}
}

二:js观察者模式

Observer模式是行为模式之一,它的作用是当一个对象的状态发生变化时,能够自动通知其他关联对象,自动刷新对象状态。

Observer模式提供给关联对象一种同步通信的手段,使某个对象与依赖它的其他对象之间保持状态同步。

包括我们平时接触的dom事件. 也是js和dom之间实现的一种观察者模式.
只要订阅了div的click事件. 当点击div的时候, function click就会被触发.

1
2
3
div.onclick = function click (){
alert ("click")
}

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
/**
* 发布订阅模式(观察者模式)
* handles: 事件处理函数集合
* on: 订阅事件
* emit: 发布事件
* off: 删除事件
**/

class PubSub {
constructor() {
this.handles = {};
}

// 订阅事件
on(eventType, handle) {
if (!this.handles.hasOwnProperty(eventType)) {
this.handles[eventType] = [];
}
if (typeof handle == 'function') {
this.handles[eventType].push(handle);
} else {
throw new Error('缺少回调函数');
}
return this;
}

// 发布事件
emit(eventType, ...args) {
if (this.handles.hasOwnProperty(eventType)) {
this.handles[eventType].forEach((item, key, arr) => {
item.apply(null, args);//这里的item是调用on方法时加入的函数,这里通过apply传递进去args参数进行执行。
})
} else {
throw new Error(`"${eventType}"事件未注册`);
}
return this;
}

// 删除事件
off(eventType, handle) {
if (!this.handles.hasOwnProperty(eventType)) {
throw new Error(`"${eventType}"事件未注册`);
} else if (typeof handle != 'function') {
throw new Error('缺少回调函数');
} else {
this.handles[eventType].forEach((item, key, arr) => {
if (item == handle) {
arr.splice(key, 1);
}
})
}
return this; // 实现链式操作
}
}

// 下面做一些骚操作
let callback = function () {
console.log('you are so nice');
}

let pubsub = new PubSub();
pubsub.on('completed', (...args) => {
console.log(args.join(' '));
}).on('completed', callback);

pubsub.emit('completed', 'what', 'a', 'fucking day');
pubsub.off('completed', callback);//把callback事件删除了
pubsub.emit('completed', 'fucking', 'again');

三: 强语言和弱语言

强语言要求变量的使用要严格符合定义,所有变量都必须先定义后使用。所以,一旦一个变量被指定了某个数据类型,如果不经过强制转换,那么它就永远是这个数据类型了,无法再更改了。

(java、C++)(Python属于强类型的动态脚本语言)(强类型:不予许不同类型相加/动态:不使用显示数据声明类型,且确定一个变量的类型是第一次给他赋值的时候/脚本语言:一般也是解释性语言,运行代码只需要一个解释器,不需要编译)

弱语言是数据类型可以被忽略的语言, 一个变量可以赋值给不同数据类型的值。(php,js)

四:进程和线程

说一下线程

1
2
3
多线程可以并行处理任务,线程不能单独存在,它是由进程来启动和管理的。

那么进程是什么嘞

一个进程是一个程序的运行实例。

总结:所以线程依附于进程,而进程中使用多线程并行处理能提升运算效率。

以下是进程和线程其四个特点:
1)进程中任意一线程执行出错,都会导致整个进程的崩溃。
2)线程之间共享进程中的数据。
3)当一个进程关闭之后,操作系统会回收进程所占用的内存。
4)进程之间的内容相互隔离。

那么之前单进程浏览器时代:浏览器所有的功能模块都是运行在同一个进程里,所以会出现以下几个问题:
1)不稳定
2)不流畅
3)不安全
那么对于现在多进程浏览器时代解决了上述问题,那么最新的Chrome进程架构包括:
一个浏览器主进程、一个GPU进程、一个网络进程、多个渲染进程和多个插件进程。
参考资料:
js单例模式
[]