/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/tests/javascript/javascript-let.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs · JavaScript · 79 lines · 53 code · 18 blank · 8 comment · 6 complexity · fcc4e1210965db28e9bb272ec308c862 MD5 · raw file

  1. // https://developer.mozilla.org/en/New_in_JavaScript_1.7
  2. // let statement
  3. let (x = x+10, y = 12) {
  4. print(x+y + "\n");
  5. }
  6. // let expressions
  7. print( let(x = x + 10, y = 12) x+y + "<br>\n");
  8. // let definitions
  9. if (x > y) {
  10. let gamma = 12.7 + y;
  11. i = gamma * x;
  12. }
  13. var list = document.getElementById("list");
  14. for (var i = 1; i <= 5; i++) {
  15. var item = document.createElement("LI");
  16. item.appendChild(document.createTextNode("Item " + i));
  17. let j = i;
  18. item.onclick = function (ev) {
  19. alert("Item " + j + " is clicked.");
  20. };
  21. list.appendChild(item);
  22. }
  23. function varTest() {
  24. var x = 31;
  25. if (true) {
  26. var x = 71; // same variable!
  27. alert(x); // 71
  28. }
  29. alert(x); // 71
  30. }
  31. function letTest() {
  32. let x = 31;
  33. if (true) {
  34. let x = 71; // different variable
  35. alert(x); // 71
  36. }
  37. alert(x); // 31
  38. }
  39. function letTests() {
  40. let x = 10;
  41. // let-statement
  42. let (x = x + 20) {
  43. alert(x); // 30
  44. }
  45. // let-expression
  46. alert(let (x = x + 20) x); // 30
  47. // let-definition
  48. {
  49. let x = x + 20; // x here evaluates to undefined
  50. alert(x); // undefined + 20 ==> NaN
  51. }
  52. }
  53. var x = 'global';
  54. let x = 42;
  55. document.write(this.x + "<br>\n");
  56. // let-scoped variables in for loops
  57. var i=0;
  58. for ( let i=i ; i < 10 ; i++ )
  59. document.write(i + "<br>\n");
  60. for ( let [name,value] in obj )
  61. document.write("Name: " + name + ", Value: " + value + "<br>\n");