这就是我的问题:我有两个输入域,单击按钮时,函数会被触发,使用这两个输入值(从对象构造函数)创建一个新对象。
函数中包含的console.log以正确的方式显示object.input1和object.input2。然而,一旦函数完成,并且我再次尝试控制台记录新创建的对象或object.input1或object.input1,我就会变得“未定义”
如果我将新创建的对象推入数组,这是可以的,我可以从数组访问对象,但不确定在数组中如何访问对象元素。无论如何,我不明白我在创建新对象时做错了什么。任何帮助都将不胜感激。
<html>
<input type="number" id='input1' placeholder="Enter number only">
<input type="text" id='input2' placeholder="Max length 300" maxlength='300'>
<button id='btn'>Submit</button>
<script>
var day1, notes1, day1List;
function DayList(day, notes) {
this.day = day;
this.notes = notes;
}
document.getElementById('btn').onclick =
function() {
var day1 = document.getElementById('input1').valueAsNumber;
var notes1 = document.getElementById('input2').value;
var day1List = new DayList(day1, notes1);
console.log(day1List.day, day1List.notes);
}
</script>
</html>
https://stackoverflow.com/questions/51974088
复制相似问题