gpt4 book ai didi

javascript - 当 obj 中的键被转换为 __indexOf 与 Coffeescript 的用法时

转载 作者:行者123 更新时间:2023-11-29 20:00:52 25 4
gpt4 key购买 nike

这一茶匙咖啡......

    _pickInConf = (sourceConf,propsToPick...) ->
newConfWithPickedProperties = {}
newConfWithPickedProperties[key] = sourceConf[key] for key in Array::.concat.apply Array::,propsToPick when key in sourceConf
newConfWithPickedProperties

... 被编译成:

_pickInConf = function() {
var key, newConfWithPickedProperties, propsToPick, sourceConf, _i, _len, _ref;
sourceConf = arguments[0], propsToPick = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
newConfWithPickedProperties = {};
_ref = Array.prototype.concat.apply(Array.prototype, propsToPick);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
key = _ref[_i];
if (__indexOf.call(sourceConf, key) >= 0) {
newConfWithPickedProperties[key] = sourceConf[key];
}
}
return newConfWithPickedProperties;
};

...并利用:

__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; };

我希望 Coffeescript 编译器能够转译这个 block :

when key in sourceConf

进入:

if (key in sourceConf) {

...这就是我要用 JS 编写代码的意思...

有没有办法强制 Coffescript 输出这种 JS?或者让它理解 sourceConf 是一个对象,而不是一个数组?

预先感谢您的回答!

最佳答案

您希望 of 而不是 in 来迭代对象的键:http://coffeescript.org/#loops (接近本节末尾)

对此进行扩展:

for a in items when a of obj
stuff

编译为

var a, _i, _len;

for (_i = 0, _len = items.length; _i < _len; _i++) {
a = items[_i];
if (a in obj) {
stuff;

}
}

它正在使用你正在寻找的 a in obj

关于javascript - 当 obj 中的键被转换为 __indexOf 与 Coffeescript 的用法时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14610230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com