上药三品,神与气精

曾因酒醉鞭名马 生怕情多累美人


  • 首页

  • 关于

  • 分类

  • 标签

  • 归档

  • 搜索

js0019

发表于 2018-12-15 | 阅读次数:
字数统计: 162 | 阅读时长 ≈ 1

作为生成器函数参数发送值

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

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sending data to and recieving data from a generator</title>
<link rel="stylesheet" href="../assert.css">
<script src="../assert.js"></script>
</head>
<body>
<script>
function* NinjaGenerator(action) {
const imposter = yield ("Hatori " + action);

assert(imposter === "Hanzo",
"The generator has been infiltrated");

yield ("Yoshi (" + imposter + ") " + action);
}
 
const ninjaIterator = NinjaGenerator("skulk");
 
const result1 = ninjaIterator.next();
assert(result1.value === "Hatori skulk", "Hatori is skulking");

const result2 = ninjaIterator.next("Hanzo");
assert(result2.value === "Yoshi (Hanzo) skulk",
"We have an imposter!");
</script>
</body>
</html>

js0018

发表于 2018-12-15 | 阅读次数:
字数统计: 148 | 阅读时长 ≈ 1

通过函数访问私有变量 而不是通过对象访问

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

<!DOCTYPE html>
<html>
<head>
<title>Private variables are accessed through functions and not through objects</title>
<meta charset="utf-8">
<script src="../assert.js"></script>
<link rel="stylesheet" type="text/css" href="../assert.css">
</head>
<body>
<script>
function Ninja() {
var feints = 0;
this.getFeints = function(){
return feints;
};
this.feint = function(){
feints++;
};
}
var ninja1 = new Ninja();
ninja1.feint();

var imposter = {};
imposter.getFeints = ninja1.getFeints;

assert(imposter.getFeints () === 1,
"The imposter has access to the feints variable!");
</script>
</body>
</html>

js0017

发表于 2018-12-15 | 阅读次数:
字数统计: 199 | 阅读时长 ≈ 1

对比上一个 使用const let 关键字

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
<!DOCTYPE html>
<html>
<head>
<title>Using const and let keywords</title>
<meta charset="utf-8">
<script src="../assert.js"></script>
<link rel="stylesheet" type="text/css" href="../assert.css">
</head>
<body>
<script>
"use strict"
const globalNinja = "Yoshi";

function reportActivity(){
const functionActivity = "jumping";

for(let i = 1; i < 3; i++) {
let forMessage = globalNinja + " " + functionActivity;
assert(forMessage === "Yoshi jumping",
"Yoshi is jumping within the for block");
assert(i, "Current loop counter:" + i);
}

assert(typeof i === "undefined" && typeof forMessage === "undefined",
"Loop variables not accessible outside the loop");
}

reportActivity();
assert(typeof functionActivity === "undefined"
&& typeof i === "undefined" && typeof forMessage === "undefined",
"We cannot see function variables outside of a function");
</script>
</body>
</html>

js0016

发表于 2018-12-15 | 阅读次数:
字数统计: 233 | 阅读时长 ≈ 1

使用关键字var时 该变量时在距离最近的函数内部或是在全局词法环境中定义的

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
<!DOCTYPE html>
<html>
<head>
<title>Using the var keyword</title>
<meta charset="utf-8">
<script src="../assert.js"></script>
<link rel="stylesheet" type="text/css" href="../assert.css">
</head>
<body>
<script>
var globalNinja = "Yoshi";

function reportActivity(){
var functionActivity = "jumping";

for(var i = 1; i < 3; i++) {
var forMessage = globalNinja + " " + functionActivity;
assert(forMessage === "Yoshi jumping",
"Yoshi is jumping within the for block");
assert(i, "Current loop counter:" + i);
}

assert(i === 3 && forMessage === "Yoshi jumping",
"Loop variables accessible outside of the loop");
}

// for 循环外部 仍然能够访问for循环中定义的变量

reportActivity();
assert(typeof functionActivity === "undefined"
&& typeof i === "undefined" && typeof forMessage === "undefined",
"We cannot see function variables outside of a function");
</script>
</body>
</html>

js0015

发表于 2018-12-15 | 阅读次数:
字数统计: 129 | 阅读时长 ≈ 1

执行上下文来跟踪代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html>
<html>
<head>
<title>The creation of execution contexts</title>
<meta charset="utf-8">
<script src="../assert.js"></script>
<link rel="stylesheet" type="text/css" href="../assert.css">
</head>
<body>
<script type="text/javascript">
function skulk(ninja) {
report(ninja + " skulking");
}

function report(message) {
console.log(message);
}

skulk("Kuma");
skulk("Yoshi");
</script>
<p>Outputs to the console (press F12)</p>
</body>
</html>
1…616263…109
John Cheung

John Cheung

improve your python skills

543 日志
33 分类
45 标签
RSS
GitHub Email
© 2020 John Cheung
本站访客数:
|
主题 — NexT.Pisces v5.1.4
博客全站共226.3k字