Coffeescript中的switch case语句内的范围

use*_*non 5 coffeescript handlebars.js

我在我的Rails 3.2 jquery移动应用程序中使用Handlebar.

我试图在Coffeescript方法中编写一个switch case语句

Handlebars.registerHelper 'status', (blog) ->
  switch(parseInt(blog.status))
    when [0..20]
      status = "active"
    when [20..40]
      status = "Moderately Active"
    when [40..60]
      status = "Very Active"
    when [60..100]
      status = "Hyper Active"
  return status
Run Code Online (Sandbox Code Playgroud)

我没有得到任何结果.如何在何时使用范围.请建议

mu *_*ort 16

在评论中你switch不会像Cygal那样工作(即参见问题1383).A switch只是一个美化的if(a == b)结构,你需要能够说出这样的话:

a = [1,2,3]
switch a
...
Run Code Online (Sandbox Code Playgroud)

switch在阵列上工作.CoffeeScript设计师认为添加一个(脆弱的)特殊情况来处理数组(这就是全部[a..b])是不值得的.

你可以用if:

Handlebars.registerHelper 'status', (blog) ->
  status = parseInt(blog.status, 10)
  if 0 <= status <= 20
    'Active'
  else if 20 < status <= 40
    'Moderately Active'
  else if 40 < status <= 60
    'Very Active'
  else if 60 < status <= 100
    'Hyper Active'
  else
    # You need to figure out what to say here
Run Code Online (Sandbox Code Playgroud)

或者return像这样的短路:

Handlebars.registerHelper 'status', (blog) ->
  status = parseInt(blog.status, 10)
  return 'Something...'      if status <=   0
  return 'Active'            if status <=  20
  return 'Moderately Active' if status <=  40
  return 'Very Active'       if status <=  60
  return 'Hyper Active'      if status <= 100
  return 'Something else'    # This return isn't necessary but I like the symmetry
Run Code Online (Sandbox Code Playgroud)

请注意,您需要为以下三个特殊情况添加字符串:

  1. status < 0.
  2. status > 100.
  3. statusNaN.这种情况通常属于最终的"它不小于或等于100"分支NaN => n,NaN <= n因此对所有人都是错误的n.

是的,你绝对肯定状态总是在假定的范围内.另一方面,不可能一直发生软件(因此comprisrisks邮件列表)并且没有充分的理由留下容易填充的漏洞.

还要注意在parseInt调用中添加了radix参数,你不会想要一个前导零来搞乱一些东西.是的,radix参数是可选的,但它确实不应该是,你的手指应该自动添加, 10parseInt你做的每个电话.


epi*_*ian 8

mu添加一小部分是太短的答案,您可以将其第二个代码片段转换为switch表达式:

Handlebars.registerHelper 'status', (blog) ->
  status = parseInt(blog.status, 10)
  switch
    when status <= 0   then 'Something...'      
    when status <= 20  then 'Active'
    when status <= 40  then 'Moderately Active'
    when status <= 60  then 'Very Active'
    when status <= 100 then 'Hyper Active'
    else 'Something else'
Run Code Online (Sandbox Code Playgroud)

这基本上等同于switch (true)在JavaScript中执行(尽管CS编译器将生成一个switch (false)带有否定条件语句,以确保表达式的布尔结果......我认为).


并且switch超范围不起作用的原因是CS中的范围文字表示普通的旧JS数组(虽然编译器在执行某些操作时会做一些优化技巧for i in [1..something]),所以当它们在内部被发现时,它们就像正常一样switch对待数组值:

// Generated JS for question's CS code:
switch (parseInt(blog.status)) {
  case [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]:
    status = "active";
    break;
  case [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]:
    status = "Moderately Active";
    break;
  case [40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]:
    status = "Very Active";
    break;
  case (function() {
      _results = [];
      for (_i = 60; _i <= 100; _i++){ _results.push(_i); }
      return _results;
    }).apply(this):
    status = "Hyper Active";
}
Run Code Online (Sandbox Code Playgroud)

switch语句中的值基本上与使用的每个case值进行比较===,这仅适用于基元,而不适用于数组(即使它适用于数组,它也会测试数组相等性,而不是ed数组中switch包含ed值case) .