JavaScript的`关键字(for ...的循环) [英] JavaScript `of` Keyword (for...of loops)

查看:170
本文介绍了JavaScript的`关键字(for ...的循环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现,Firefox SDK中的 JavaScript (在MDN上),使用一个我从未见过的关键字:

  var tabs = require(' SDK /标签); 
(让标签的tab)
console.log(tab.title);

是由Mozilla或者是标准化的?

...循环,属性值是添加到EcmaScript 6中的JavaScript规范

考虑到这个问题的上下文是一个Firefox附加组件,问题不是在何时,或者是否在其他浏览器中可用。问题出在这个 EcmaScript 6功能被添加到Firefox 和任何使用它的向后兼容限制。

它被添加到Firefox的 Firefox 13 。因此,使用它将导致您的附件限制为Firefox 13+。鉴于目前的版本,截至2014年10月,是Firefox 33.0,并且在Firefox 13和现在之间已经有多个ESR版本,使用for ... for循环可能不会显着减少能够使用加上。您正在使用的其他功能可能会将您的加载项限制为更新的版本。



如果您打算将加载项移植到其他浏览器,那么你应该知道什么时候该功能被添加到他们。从浏览器中可以看出MDN上的兼容表,主要问题是它不被Internet Explorer支持。



for ...的循环并不仅限于数组,而且将迭代其他类型的可迭代对象
如果您正在寻找其他方式执行类似的任务, MDN显示了使用 Array.prototype.forEach()遍历 Arrays and 对象

  let arr = [3,5,7]; 
let obj = {first:3,second:5,third:7,fourth:hello];
$ b arr.forEach(function(element,index){
console.log(element); //记录3,5,7
控制台。 log(index); //记录0,1,2
});

with Object.keys()

Object.keys(obj).forEach(function(key){
// obj [key]属性值
console.log(obj [key]); // logs3,5,7,hello
});

传统的兼容性: 对于内容脚本中的循环(即不作为附加组件),这些是 MDN兼容性表格截至2014-11-02: https://i.stack.imgur.com/CM9yj.pngalt =桌面浏览器兼容性>



1. Chrome 29-37:for ... of循环功能位于首选项后面。在chrome:// flags中,激活条目Enable Experimental JavaScript。


I just spotted, in Firefox SDK JavaScript (on MDN), the use of a keyword I have never seen before:

var tabs = require('sdk/tabs');
for (let tab of tabs)
  console.log(tab.title);

Is the of keyword made up by Mozilla or is it standardized?

解决方案

The for...of loop, which iterates over property values, is a feature being added to the JavaScript specification in EcmaScript 6.

Given that the context of this question is a Firefox add-on, the issue is not when, or if, it is available in other browsers. The issue is when this EcmaScript 6 feature was added to Firefox and any limitation on backward compatibility which using it causes.

It was added to Firefox in Firefox 13. Thus, using it will result in restricting your add-on to be Firefox 13+. Given that the current release, as of October 2014, is Firefox 33.0 and there have been multiple ESR releases between Firefox 13 and now, using a for...of loop probably will not significantly reduce the number of people who are able to use your add-on. It is likely that some other feature which you are using will restrict your add-on to a more recent version.

If you are intending to port your add-on to other browsers, then you should be aware of when the feature was added to them. As can be seen in the Browser Compatibility table on MDN, the primary issue is that it is not supported by Internet Explorer.

Unlike Array.prototype.forEach(), for...of loops are not restricted to only Arrays and will iterate over other types of iterable objects. If you are looking for other ways to perform a similar task, MDN shows examples of using Array.prototype.forEach() to iterate over property values for Arrays and Objects:

let arr = [ 3, 5, 7 ];
let obj = { first: 3, second: 5, third: 7, fourth: "hello" ];

arr.forEach(function (element, index) {
    console.log(element); // logs "3", "5", "7"
    console.log(index);   // logs "0", "1", "2"
});

// with Object.keys()

Object.keys(obj).forEach(function (key) {
    //obj[key] is the property value
    console.log(obj[key]); // logs "3", "5", "7", "hello"
});

Broswer compatibility:
For those using for...of loops in content scripts (i.e. not as an add-on), these are the compatibility tables from MDN as of 2014-11-02:



1. Chrome 29–37: The for...of loop feature is available behind a preference. In chrome://flags, activate the entry "Enable Experimental JavaScript".

这篇关于JavaScript的`关键字(for ...的循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆