admin管理员组文章数量:1291045
I'm using the following code in CoffeeScript:
if elem in my_array
do_something()
Which piles to this 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.
My question is regarding this use case: I want to remove an element from an array, and I want to support IE8. I can do that easily with indexOf
and splice
in browsers who support indexOf
on an array
object. However, in IE8 this doesn't work:
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 piler.
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? It seems weird to define the same function twice, just because CoffeeScript won't let me use theirs...
I'm using the following code in CoffeeScript:
if elem in my_array
do_something()
Which piles to this 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.
My question is regarding this use case: I want to remove an element from an array, and I want to support IE8. I can do that easily with indexOf
and splice
in browsers who support indexOf
on an array
object. However, in IE8 this doesn't work:
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 piler.
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? It seems weird to define the same function twice, just because CoffeeScript won't let me use theirs...
Share Improve this question asked Jun 21, 2012 at 14:29 CristianCristian 6,0856 gold badges47 silver badges57 bronze badges 1- possible duplicate of Clean way to remove element from javascript array (with jQuery, coffeescript) – Mohammed Azharuddin Shaikh Commented Jun 22, 2012 at 6:03
2 Answers
Reset to default 7No, CoffeeScript precludes you from using its helpers directly, since that would break down the distinction between the language and the implementation. To support IE8, I would add a shim like
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.
CoffeeScript adds the following to the top of file scope:
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 piler error: RESERVED WORD "__INDEXOF"
The solution is to subvert the piler 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:
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). At the end of the day, it might just be easier for some just to redefine it :)
本文标签: javascriptUsing indexOf in CoffeeScriptStack Overflow
版权声明:本文标题:javascript - Using indexOf in CoffeeScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741506915a2382375.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论