DOM4 使 NodeList可迭代:
interface <i>NodeList</i> {
getter <a href="https://www.w3.org/TR/dom/#node" rel="noreferrer noopener nofollow">Node</a>? <a href="https://www.w3.org/TR/dom/#dom-nodelist-item" rel="noreferrer noopener nofollow">item</a>(unsigned long <i>index</i>);
readonly attribute unsigned long <a href="https://www.w3.org/TR/dom/#dom-nodelist-length" rel="noreferrer noopener nofollow">length</a>;
iterable<<a href="https://www.w3.org/TR/dom/#node" rel="noreferrer noopener nofollow">Node</a>>;
};
根据 WebIDL , 这意味着
Objects implementing an interface that is declared to be iterable support being iterated over to obtain a sequence of values.
Note: In the ECMAScript language binding, an interface that is iterable will have “entries”, “forEach”, “keys”, “values” and @@iterator properties on its interface prototype object.
所以以下是可能的:
for (var el of document.querySelectorAll(selector)) ...
我注意到在 Firefox 和 Chrome 上,HTMLCollections 似乎同样有效:
for (var el of document.getElementsByTagName(tag)) ...
其实我明白了
HTMLCollection.prototype[Symbol.iterator] === [][Symbol.iterator]; // true
然而,HTMLCollection未定义为可迭代的:
interface <i>HTMLCollection</i> {
readonly attribute unsigned long <a href="https://www.w3.org/TR/dom/#dom-htmlcollection-length" rel="noreferrer noopener nofollow">length</a>;
getter <a href="https://www.w3.org/TR/dom/#element" rel="noreferrer noopener nofollow">Element</a>? <a href="https://www.w3.org/TR/dom/#dom-htmlcollection-item" rel="noreferrer noopener nofollow">item</a>(unsigned long <i>index</i>);
getter <a href="https://www.w3.org/TR/dom/#element" rel="noreferrer noopener nofollow">Element</a>? <a href="https://www.w3.org/TR/dom/#dom-htmlcollection-nameditem" rel="noreferrer noopener nofollow">namedItem</a>(DOMString <i>name</i>);
};
我还检查了 WHATWG DOM spec它也不是可迭代的。
那么,这种行为规范不规范呢? HTMLCollection 应该在原型(prototype)中有一个 @@iterator 吗?
最佳答案
我找到了,在WebIDL中有解释:
If the interface has any of the following:
- an iterable declaration
- an indexed property getter and an integer-typed attribute named “length”
- a maplike declaration
- a setlike declaration
then a property must exist whose name is the @@iterator symbol, with attributes { [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true } and whose value is a function object. [...]
If the interface defines an indexed property getter, then the Function object is %ArrayProto_values%.
在这种情况下,HTMLCollections 有一个索引属性 getter:
getter <a href="https://www.w3.org/TR/dom/#element" rel="noreferrer noopener nofollow">Element</a>? <a href="https://www.w3.org/TR/dom/#dom-htmlcollection-item" rel="noreferrer noopener nofollow">item</a>(unsigned long <i>index</i>);
和一个名为“length”的整数类型属性:
readonly attribute unsigned long <a href="https://www.w3.org/TR/dom/#dom-htmlcollection-length" rel="noreferrer noopener nofollow">length</a>;
因此,是的,它应该有效。事实上,它也适用于 NodeLists,即使它们没有被声明为可迭代的,但它们不会有 entries、forEach、keys 和 values 属性。正如@lonesomeday 提到的,很可能 HTMLCollection 没有定义为可迭代的,因为添加这些方法不会向后兼容,因为 namedItem getter 接受任意字符串。
关于javascript - HTMLCollections 可以用 for...of (Symbol.iterator) 迭代吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41758982/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的
似乎无法为此找到有效的答案。我正在阅读Rails教程的第10章第10.1.2节,但似乎无法使邮件程序预览正常工作。我发现处理错误的所有答案都与教程的不同部分相关,我假设我犯的错误正盯着我的脸。我已经完成并将教程中的代码复制/粘贴到相关文件中,但到目前为止,我还看不出我输入的内容与教程中的内容有什么区别。到目前为止,建议是在函数定义中添加或删除参数user,但这并没有解决问题。触发错误的url是http://localhost:3000/rails/mailers/user_mailer/account_activation.http://localhost:3000/rails/mai
我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法
我们的git存储库中目前有一个Gemfile。但是,有一个gem我只在我的环境中本地使用(我的团队不使用它)。为了使用它,我必须将它添加到我们的Gemfile中,但每次我checkout到我们的master/dev主分支时,由于与跟踪的gemfile冲突,我必须删除它。我想要的是类似Gemfile.local的东西,它将继承从Gemfile导入的gems,但也允许在那里导入新的gems以供使用只有我的机器。此文件将在.gitignore中被忽略。这可能吗? 最佳答案 设置BUNDLE_GEMFILE环境变量:BUNDLE_GEMFI
当我在我的Rails应用程序根目录中运行rakedoc:app时,API文档是使用/doc/README_FOR_APP作为主页生成的。我想向该文件添加.rdoc扩展名,以便它在GitHub上正确呈现。更好的是,我想将它移动到应用程序根目录(/README.rdoc)。有没有办法通过修改包含的rake/rdoctask任务在我的Rakefile中执行此操作?是否有某个地方可以查找可以修改的主页文件的名称?还是我必须编写一个新的Rake任务?额外的问题:Rails应用程序的两个单独文件/README和/doc/README_FOR_APP背后的逻辑是什么?为什么不只有一个?
我喜欢使用Textile或Markdown为我的项目编写自述文件,但是当我生成RDoc时,自述文件被解释为RDoc并且看起来非常糟糕。有没有办法让RDoc通过RedCloth或BlueCloth而不是它自己的格式化程序运行文件?它可以配置为自动检测文件后缀的格式吗?(例如README.textile通过RedCloth运行,但README.mdown通过BlueCloth运行) 最佳答案 使用YARD直接代替RDoc将允许您包含Textile或Markdown文件,只要它们的文件后缀是合理的。我经常使用类似于以下Rake任务的东西: