简体   繁体   English

javascript + coffeescript,具有分组运算符?

[英]javascript + coffeescript, with grouping operator?

Is there grouping operator for coffeescript? 有咖啡脚本的分组运算符吗? I'm trying to turn this: 我正试图扭转这个:

var urlParams;
(window.onpopstate = function () {
    var match,
        pl     = /\+/g,  // Regex for replacing addition symbol with a space
        search = /([^&=]+)=?([^&]*)/g,
        decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
        query  = window.location.search.substring(1);

    urlParams = {};
    while (match = search.exec(query))
       urlParams[decode(match[1])] = decode(match[2]);
})();

into coffeescript. 变成咖啡脚本。 I have this so far, but I'm not getting it correct 到目前为止,我已经知道了,但是我没有正确

window.onpopstate = ->
  pl     = /\+/g  # Regex for replacing addition symbol with a space
  search = /([^&=]+)=?([^&]*)/g
  decode = (s) ->
    return decodeURIComponent(s.replace(pl, " "))
  query  = window.location.search.substring(1)

  urlParams = {};
  while (match = search.exec(query))
    urlParams[decode(match[1])] = decode(match[2]);

Trying to alert, but getting urlParams is not defined 尝试发出警报,但urlParams is not defined

jQuery ->
  alert(urlParams['hair'])

It might be because of the () at the end of the function that I'm not getting it? 可能是由于函数末尾的()导致我没有得到它?

CoffeeScript will declare variables in the scope at their first assignment. CoffeeScript将在第一次分配时在作用域中声明变量。 Since you first assign urlParams within onpopstate , that's where it's declared, and is not accessible outside there. 自从您第一次在urlParams内分配urlParams onpopstate ,这就是它的声明位置,并且在那里无法访问。 To replicate the JavaScript's behavior, you've got to initialize it outside of the onpopstate handler: 要复制JavaScript的行为,您必须在onpopstate处理程序之外对其进行初始化:

urlParams = null
window.onpopstate = ->
  # ...

Note that this assumes that urlParams will only be accessed within that one script; 请注意,这假设urlParams仅在该一个脚本内访问; since CoffeeScript wraps everything in an IIFE and urlParams will be trapped inside it, you cannot access it outside if declared that way. 由于CoffeeScript将所有内容包装在IIFE中,并且urlParams将被捕获​​在其中,因此,如果以这种方式声明,则无法在外部访问它。 If you want it to be truly global, accessible from other scripts as well, you'll need to replace every occurrence with window.urlParams . 如果您希望它确实是全局的,并且也可以从其他脚本访问,则需要用window.urlParams替换每次出现的window.urlParams

A few other things, though: 不过,还有一些其他事情:

  1. The () at the end of the JavaScript version is indeed not there in the CoffeeScript version, although that's not what caused your problem. JavaScript版本末尾的()实际上不在CoffeeScript版本中,尽管这不是造成您问题的原因。 Fortunately, there's a very easy way to do this in CoffeeScript without adding a bunch of parentheses: use do : 幸运的是,在CoffeeScript中有一种非常简单的方法,而无需添加括号:use do

     do window.onpopstate = -> # ... 
  2. Trailing semicolons are not required in CoffeeScript. CoffeeScript中不需要结尾的分号。 I'd remove them. 我将其删除。

  3. That return in the definition of decode is implicit in CoffeeScript. CoffeeScript中隐含了decode定义的return I'd remove it. 我将其删除。

  4. Parentheses are not required around the condition of the while . while的条件附近不需要括号。 I'd remove them. 我将其删除。

  5. If you'd like, you can take advantage of CoffeeScript's implicit function calls without parentheses, eg: 如果愿意,您可以利用CoffeeScript的不带括号的隐式函数调用,例如:

     while match = search.exec query 

    There's several other places you could apply this too, although for readability's sake I'd only do it where it seems sensible. 您也可以在其他几个地方应用此方法,尽管出于可读性考虑,我只会在看起来合理的地方进行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM