Live Note

Remain optimistic

函数的默认值

在 ES6 之前,不能直接为函数指定默认值,只能采用变通的方法:

1
2
3
4
5
6
7
8
function log(x, y) {
if (y === "undefined") {
y = "world"
}
console.log(x, y)
}
log("hello") //hello test
log("hello", "") //hello

ES6 允许为函数的参数设置默认值:

1
2
3
4
5
6
7
8
9
10
11
12
function log(x, y = "world") {
console.log(x, y)
}
log("hello") //hello world
log("hello", "") //hello

eg: function Point(x = 0, y = 0) {
this.x = x
this.y = y
}
let p = new Point()
console.log(p.x + " " + p.y) //0 0
Read more »

Factory

缺点:对象无法区分,所有的实例指向一个原型

1
2
3
4
5
6
7
8
9
10
11
12
13
function Person(name) {
let o = new Object()
o.name = name
o.getName = function () {
return this.name
}

return o
}

let person = new Person("Edward")
console.log(person instanceof Person) // false
console.log(person instanceof Object) // true
Read more »

Functional Programming

经过这几天的了解,越觉得 FP 十分有趣
给个例子:

1
2
3
4
5
6
7
8
// a simple function
function add(a, b) {
return a + b
}
/// the same as
let add = function (a, b) {
return a + b
}

在 ES6 中,存在着箭头函数。所以上面的函数可以写成这个形式:

1
let add = (a, b) => a + b
Read more »

同源策略

  • 同源策略(MDN 解释):限制从一个源加载的文档或脚本如何与来自另一个源的资源进行交互。这是一个用于隔离潜在恶意文件的关键的安全机制。
  • 源:协议、域名、端口。
  • 限制:无法获取 Cookie 、 LocalStorage 和 IndexDB ,无法操作 dom ,不能发送 Ajax 。
Read more »

.gitignore 文件的配置

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
# Numerous always-ignore extensions

_.bak
_.patch
_.diff
_.err

# temp file for git conflict merging

_.orig
_.log
_.rej
_.swo
_.swp
_.zip
_.vi
_~
_.sass-cache
_.tmp.html
\*.dump

# OS or Editor folders

.DS*Store
.*_
.cache
.project
.settings
.tmproj
_.esproj
_.sublime-project
_.sublime-workspace
nbproject
thumbs.db
\*.iml

# Folders to ignore

.hg
.svn
.CVS
.idea
node_modules/
jscoverage_lib/
bower_components/
dist/