js0019 发表于 2018-12-15 | 阅读次数: 字数统计: 162 | 阅读时长 ≈ 1 作为生成器函数参数发送值 12345678910111213141516171819202122232425262728293031<!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 通过函数访问私有变量 而不是通过对象访问 12345678910111213141516171819202122232425262728293031<!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 关键字 12345678910111213141516171819202122232425262728293031323334<!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时 该变量时在距离最近的函数内部或是在全局词法环境中定义的 1234567891011121314151617181920212223242526272829303132333435<!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 执行上下文来跟踪代码123456789101112131415161718192021222324<!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>