Live Note

Remain optimistic

Iterator 概念

Iterator 是一种接口,为各种不同的数据结构提供统一的访问机制。任何数据结构,只要部署 Iterator 接口,就可以完成遍历操作。
Iterator 的主要作用:为数据结构提供统一的、简便的访问接口;使得数据结构的成员能够按照某种次序排列;供 for…of 消费。
遍历过程如下:

  1. 创建一个指针对象,指向当前数据结构的起始位置。
  2. 第一次调用指针对象的 next 方法,将指针指向数据结构的第一个成员。
  3. 第二次调用 next 方法,指向第二个成员。
  4. 不断调用 next 方法,直到指针指向数据结构的结束位置。

每次调用 next 方法都会返回数据结构当前成员的信息,返回一个包含 value 的 done 两个属性的对象。value 属性是当前成员的值,done 属性是一个布尔值,表示遍历是否结束。
模拟 next 方法返回值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var it = makeIterator(['a', 'b'])
it.next() // { value : 'a', done : false }
it.next() // { value : 'b', done : false }
it.next() // { value : undefined, done : true }
function makeIterator(array) {
var nextIndex = 0
return {
next: function () {
return nextIndex < array.length
? { value: array[nextIndex++], done: false }
: { value: undefined, done: true }
},
}
}

遍历器与所遍历的数据结构实际上是分开的,完全可以写出没有对应数据结构的遍历器对象,或者用遍历器对象模拟出数据结构。无限运行的遍历器对象的例子:

1
2
3
4
5
6
7
8
9
10
11
12
var it = idMaker()
it.next().value // 0
it.next().value // 1
//...
function idMaker() {
var index = 0
return {
next: function () {
return { value: index++, done: false }
},
}
}
Read more »

Class in ES2015

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* Class representing a point.
*/
class Point {
/**
* Create a point.
* @param {number} x - The x value.
* @param {number} y - The y value.
*/
constructor(x, y) {}

/**
* Get the x value.
* @return {number} The x value.
*/
getX() {}

/**
* Convert a string containing two comma-separated number into a point.
* @param {string} str - The string containing two comma-separated number.
* @return {Point} A point object.
*/
static fromStringStr(str) {}
}

extends

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* Class representing a point.
* @extends Point
*/
class Dot extends Point {
/**
* Create a dot.
* @param {number} x - The x value.
* @param {number} y - The y value.
* @param {number} width - The width of the dot.
*/
constructor(x, y, width) {
super(x, y)
}

/**
* Get the width of the dot.
* @return {number} The dot's width.
*/
getWidth() {}
}

@abstract

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
/**
* Foo.
* @constructor
*/
function Foo() {}

/**
* Check if is solid.
* @abstract
* @return {boolean}
*/
Foo.prototype.isSolid = function () {
throw new Error("Must be implemented by suclass.")
}

/**
* Bar.
* @constructor
* @arguments Foo
*/
function Bar() {}

/**
* Check if is solid.
* @return {boolean} Always return false.
*/
Bar.prototype.isSolid = function () {
return false
}

@assets

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
/**
* @constructor
*/
function Foo() {
/**
* @assets private
* @type {number}
*/
let foo = 0

/**
* @assets protected
* @type {string}
*/
this.bar = "1"

/**
* @assets package
* @type {string}
*/
this.barz = "2"

/**
* @assets public
* @type {string}
*/
this.barm = "3"
}

@author

1
2
3
4
/**
* @author Edward <wang.huiyang@outlook.com>
*/
function MyClass() {}

@callback

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* @class
*/
function Foo() {}

/**
* Send a request.
* @param {requestCallback} cb - The callback than handles the response.
*/
Foo.prototype.send = function (cb) {}

/**
* Callback
* @callback requestCallback
* @param {number} responseCode
* @param {string} responseMessage
*/

@event

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* @constructor
*/
function Foo() {}

/**
* Do some test.
* @fires Foo#test
*/
Foo.prototype.test = function () {}

/**
* Foo test.
* @event Foo#test
* @type {object}
* @property {boolean} isPass - Check if is pass.
*/

eval()

eval 可以解析任何字符串变成 JS:

1
2
3
var str = "function testFunction() {console.log('test');}"
eval(str)
testFunction() //'test'

JSON.parse()

JSON.parse 只能解析 JSON 形式的字符串变成 JS,安全性比 eval 高一些。
字符串中的属性要严格加上引号

1
2
3
let str = '{ "name" : "hello" }'
let json = JSON.parse(str)
json.name //'hello'
Read more »

Linux 常用命令

  • man: an interface to the on-line reference manuals
  • su [options] [username]: change user ID or become superuser
  • useradd [options] LOGIN: create a new user or update default new user information
  • userdel [options] LOGIN: delete a user account and related files
  • passwd [options] [LOGIN]: change user password
  • ps [options]: report a snapshot of the current process.
  • kill [options] […]: send a signal to a process
  • fdisk [options] device: manipulate disk partition table
  • mount: mount a filesystem
  • chown [OPTION] … [OWNER]:[GROUP]] FILE …: change file owner and group
  • chgrp [OPTION] … GROUP FILE …: change group ownership
  • chmod [OPTION] … MODE[,MODE] … FILE …: change file mode bits
  • grep [OPTION] PATTERN [FILE…]: print lines matching a pattern
  • find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point…] [expression]: search for files in a directory hierarchy
  • ln: make links between files
  • gzip, gunzip, zcat: compress or expand files
  • tar: an archiving utility
  • diff [OPTION] … FILES: compare files line by line
  • patch -pnum < patchfile: apply a diff file to an original
  • ifconfig: configure a network interface

要求

父进程创建 3 个进程,父进程等待子进程 2 运行完成之后,自行退出;其中子进程 1 运行系统命令“cp /bin/ls /tmp”;等待 2 秒后退出;子进程 2 使用标准 I/O 函数打开文件 src_file,向其内写入“this is process 2\n”,之后等待 5 秒后退出;子进程 3 处理为守护进程,每隔 5 秒向日志文件/var/log/messages 写入“this is process 3\n”

提交形式

  1. 代码
  2. 运行结果截图
Read more »