English 中文(简体)
关闭咖啡包中的私人变量
原标题:Using closure for private variables in coffeescript

在 Java文中,人们将界定一个私人成员变量,使之成为一个地方变数,从而恢复封闭功能:

var count = (function(){
  var i = 0;
  return function (){ return i++; }
})();

这涉及在 Java文中相当常见的固定功能-星座,但我不知道如何在咖啡厅翻译。 任何想法?

最佳回答

You can use the do keyword

count = do ->
  i = 0
  -> i++
问题回答

正如Brian所说的那样,do 关键词最好。 你也可以使用教区,如 Java:

count = (->
  i = 0
  -> i++
)()




相关问题
Closure/scope JavaScript/jQuery

I m trying to group some exisiting top-level functions inside a closure (to avoid polluting the global namespace) but I m not quite getting it to work. First, all the JS works outside my anonymous ...

Nested Lambdas in Python

I m a beginning python programmer, and I d like someone to clarify the following behavior. I have the following code: env = lambda id: -1 def add(id, val, myenv): return lambda x: val if x == ...

closures mean fully type-safe criteria?

combining closures (FCM) and generics, would it be possible to have fully type-safe criteria. // The following works without a cast as Foo.id is a long field. List<Long> ids = session....

Is nested XMLHttpRequests with multiple closures a good idea?

I have a Greasemonkey script which operates on a search results page at a video site. The function of the script is to take a javascript link that opens a new window with a flash player, jump through ...

How|Where are closed-over variables stored?

This is a question based on the article "Closing over the loop variable considered harmful" by Eric Lippert. It is a good read, Eric explains why after this piece of code all funcs will return the ...

Scope with a self-invoking function in Javascript

Take below code iterates over 6 input buttons and attaches an onclick event to every button that alerts the index number of the respective iteration: for (var i = 1; i < 6; ++i) { var but = ...

热门标签