javascript - Some kind of _ref usage in coffeescript? -


Consider the following situation: You do not have direct access to the array of AOs, for example

.length

Where the array [1, 2,3] is required to type again to reach its length, compiled in:

  var x, y, _i, _len, _ref; _ref = [1, 2, 3]; For (y = _i = 0, _len = _ref.length; _i & lt; _len; y = ++ _ i) {x = _ref [y]; Console.log (x, y, [1, 2, 3]. Length); }  

Where we see the variable _ref is defined.

_ref As far as I am using a coffee script in relation to 'm':

  for x, in y [ 1,2,3] console.log x, y, _ref.length  

works fine, and compile without problems

I can see I think this approach can give issues to the following situation: x / y [1,2,3,4,5,6] issued x for x, y [1,2] , 3] console.log x, y, _ref.length

where _ref is the first array in the second loop [1,2,3,4 , 5,6] , and array [1,2,3] is set to unused variables _ref1

So, are there some ways to solve this problem well ( [1,2,3] In addition to defining a variable for ?)

to reference the array To solve your underlying desire, without first putting it in a variable, you (or others, like, etc.). They receive array reference as their third argument:

  [1, 2, 3] .for (e, i, a) -> gt; In this array, console.log e, i, a  

e = entry

i = Index for this iteration

a = array reference

Do not worry about the runtime cost of the function call. Be careful that this will have a different meaning within the callback (unless you pass thisArg argument to for ). I do not know whether the coffee script gives you syntax sugar which is a non-issue (I do not use a coffee script) ...


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

java - Messages from .properties file do not display UTF-8 characters -

javascript - amcharts makechart not working -