我使用带有ES6语法的Reactjs和Babel转换器。
当我使用ES5语法时,我得到了预期的结果:
console.log(React.findDOMNode(this.refs.propertyInputs).getElementsByTagName('input'));
var propInputs = React.findDOMNode(this.refs.propertyInputs).getElementsByTagName('input');
for (var i = 0; i < propInputs.length; i++){
propInputs[i].value = "";
}
console.log输出
但是当我用于循环
时console.log(React.findDOMNode(this.refs.propertyInputs).getElementsByTagName('input'));
var propInputs = React.findDOMNode(this.refs.propertyInputs).getElementsByTagName('input');
for (let propInput of propInputs) {
propInput.value = "";
}
Babel将此代码转换为
"use strict";
var propInputs = React.findDOMNode(undefined.refs.propertyInputs).getElementsByTagName('input');
// for (var i = 0; i < propInputs.length; i++){
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = propInputs[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var propInput = _step.value;
propInput.value = "";
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator["return"]) {
_iterator["return"]();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
我收到错误
未捕获TypeError:propInputs [Symbol.iterator]不是函数