加载中...

coffeescript


其中 Y=coffeescript

源代码下载: coffeescript-cn.coffee

CoffeeScript是逐句编译为JavaScript的一种小型语言,且没有运行时的解释器。 作为JavaScript的替代品之一,CoffeeScript旨在编译人类可读、美观优雅且速度不输原生的代码, 且编译后的代码可以在任何JavaScript运行时正确运行。

参阅 CoffeeScript官方网站以获取CoffeeScript的完整教程。

  1. # CoffeeScript是一种很潮的编程语言,
  2. # 它紧随众多现代编程语言的趋势。
  3. # 因此正如Ruby和Python,CoffeeScript使用井号标记注释。
  4. ###
  5. 大段落注释以此为例,可以被直接编译为 '/ *' '* /' 包裹的JavaScript代码。
  6. 在继续之前你需要了解JavaScript的基本概念。
  7. 示例中 => 后为编译后的JavaScript代码
  8. ###
  9. # 赋值:
  10. number = 42 #=> var number = 42;
  11. opposite = true #=> var opposite = true;
  12. # 条件:
  13. number = -42 if opposite #=> if(opposite) { number = -42; }
  14. # 函数:
  15. square = (x) -> x * x #=> var square = function(x) { return x * x; }
  16. fill = (container, liquid = "coffee") ->
  17. "Filling the #{container} with #{liquid}..."
  18. #=>var fill;
  19. #
  20. #fill = function(container, liquid) {
  21. # if (liquid == null) {
  22. # liquid = "coffee";
  23. # }
  24. # return "Filling the " + container + " with " + liquid + "...";
  25. #};
  26. # 区间:
  27. list = [1..5] #=> var list = [1, 2, 3, 4, 5];
  28. # 对象:
  29. math =
  30. root: Math.sqrt
  31. square: square
  32. cube: (x) -> x * square x
  33. #=> var math = {
  34. # "root": Math.sqrt,
  35. # "square": square,
  36. # "cube": function(x) { return x * square(x); }
  37. #}
  38. # Splats:
  39. race = (winner, runners...) ->
  40. print winner, runners
  41. #=>race = function() {
  42. # var runners, winner;
  43. # winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
  44. # return print(winner, runners);
  45. #};
  46. # 存在判断:
  47. alert "I knew it!" if elvis?
  48. #=> if(typeof elvis !== "undefined" && elvis !== null) { alert("I knew it!"); }
  49. # 数组推导:
  50. cubes = (math.cube num for num in list)
  51. #=>cubes = (function() {
  52. # var _i, _len, _results;
  53. # _results = [];
  54. # for (_i = 0, _len = list.length; _i < _len; _i++) {
  55. # num = list[_i];
  56. # _results.push(math.cube(num));
  57. # }
  58. # return _results;
  59. # })();
  60. foods = ['broccoli', 'spinach', 'chocolate']
  61. eat food for food in foods when food isnt 'chocolate'
  62. #=>foods = ['broccoli', 'spinach', 'chocolate'];
  63. #
  64. #for (_k = 0, _len2 = foods.length; _k < _len2; _k++) {
  65. # food = foods[_k];
  66. # if (food !== 'chocolate') {
  67. # eat(food);
  68. # }
  69. #}

还没有评论.