简体   繁体   English

在CoffeeScript中使用indexOf

[英]Using indexOf in CoffeeScript

I'm using the following code in CoffeeScript: 我在CoffeeScript中使用以下代码:

if elem in my_array
  do_something()

Which compiles to this javascript: 哪个编译成这个javascript:

if (__indexOf.call(my_array, elem) < 0) {
  my_array.push(elem);
}

I can see it's using the function __indexOf which is defined at the top of the script. 我可以看到它使用了在脚本顶部定义的函数__indexOf。

My question is regarding this use case: I want to remove an element from an array, and I want to support IE8. 我的问题是关于这个用例:我想从数组中删除一个元素,我想支持IE8。 I can do that easily with indexOf and splice in browsers who support indexOf on an array object. 我可以使用indexOf轻松实现这一点,并在支持array对象的indexOf浏览器中进行splice However, in IE8 this doesn't work: 但是,在IE8中,这不起作用:

if (attr_index = my_array.indexOf(elem)) > -1
  my_array.splice(attr_index, 1)

I tried using the __indexOf function defined by CoffeScript but I get a reserved word error in the compiler. 我尝试使用__indexOf定义的__indexOf函数,但我在编译器中得到一个保留字错误。

if (attr_index = __indexOf.call(my_array, elem) > -1
  my_array.splice(attr_index, 1)

So how can I use CoffeScript or is there a more unobtrusive method for calling indexOf? 那么我如何使用CoffeScript或者是否有更不引人注意的方法来调用indexOf? It seems weird to define the same function twice, just because CoffeeScript won't let me use theirs... 两次定义相同的函数似乎很奇怪,因为CoffeeScript不允许我使用他们的...

No, CoffeeScript precludes you from using its helpers directly, since that would break down the distinction between the language and the implementation. 不,CoffeeScript阻止您直接使用其助手,因为这会打破语言和实现之间的区别。 To support IE8, I would add a shim like 为了支持IE8,我会添加一个垫片

Array::indexOf or= (item) ->
  for x, i in this
    return i if x is item
  return -1

or use a library like Underscore.js for array manipulation. 或使用像Underscore.js这样的库来进行数组操作。

CoffeeScript adds the following to the top of file scope: CoffeeScript将以下内容添加到文件范围的顶部:

var __indexOf = [].indexOf || function(item) {
  for (var i = 0, l = this.length; i < l; i++) {
    if (i in this && this[i] === item) return i;
  }
  return -1;
};

If we attempted to utilise this by doing: 如果我们试图通过这样做来利用它:

indexOf = __indexOf

This would produce a compiler error: RESERVED WORD "__INDEXOF" 这会产生编译错误: RESERVED WORD "__INDEXOF"

The solution is to subvert the compiler using backticks: 解决方案是使用反引号破坏编译器:

indexOf = `__indexOf`

Then use it with 然后用它

indexOf.call([1,2,3,4], 3) //2

Or we could reduce the duplicate code from @Trevor Burnham's answer: 或者我们可以从@Trevor Burnham的答案中减少重复的代码:

Array::indexOf or= `__indexOf`

However, you need to be sure CoffeeScript will be adding this definition by making use of the in operator as boolean expression (and with a dynamic length array on the right hand side). 但是,您需要确保CoffeeScript将使用in运算符作为布尔表达式(并在右侧使用动态长度数组)添加此定义。 At the end of the day, it might just be easier for some just to redefine it :) 在一天结束时,一些人可能只是更容易重新定义它:)

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

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