我正在浏览该网站上的 React.js 文档,并遇到了令我困惑的代码。
据我了解,isGoing 复选框的预填充状态将为 true(因此已选中),而 numberOfGuests 为 2。
我感到困惑的部分是handleInputChange()函数,它将变量 target设置为等于event.target——它是一个预构建的属性,返回触发事件的DOM元素。value变量允许 target.name 检索名称并将其分配给 target.checked 作为真值,将 target.value 作为假值。 “target.checked”检索 isOnGoing 的当前状态,当前状态为布尔值 true,但是“target.value”检索什么?有人可以向我解释那部分吗?
另外,只是为了确保我正确理解这一点:变量名称是否等于event.target.name还是我理解错误?
感谢您的帮助。
class Reservation extends React.Component {
constructor(props) {
super(props);
this.state = {
isGoing: true,
numberOfGuests: 2
};
this.handleInputChange = this.handleInputChange.bind(this);
}
handleInputChange(event) {
const target = event.target;
const value = target.name === 'isGoing' ? target.checked : target.value;
const name = target.name;
this.setState({
[name]: value
});
}
render() …Run Code Online (Sandbox Code Playgroud) 出于某种原因,下面操作的 doubleArray 没有显示在控制台中。在这两种情况下,我在 for 循环之后声明的任何变量都不会显示在控制台上。考虑到在第一个算法中,只有一个 for 循环,每次都递增 x。而在第二种算法中,它是一个嵌套的 for 循环。有人可以帮我解决这两种算法中的错误吗?第一种算法:
var isDuplicate = function() {
var helloWorld = [1,2,3,4,3];
var doubleValue = [];
var x = 0;
for (i = 0; i < helloWorld.length; i++) {
x = x + 1;
if (helloWorld[i] === helloWorld[x] && i !== x) {
doubleValue.push(helloWorld[i])
console.log(helloWorld[i]);
} else {
continue;
}
}
console.log(doubleValue);
};
Run Code Online (Sandbox Code Playgroud)
算法二:
var isDuplicate = function() {
var helloWorld = [1,2,3,4,3];
var doubleValue = [];
for (i = 0; i < …Run Code Online (Sandbox Code Playgroud)