window.onload = function() {
var Employee = function(name, bd) {
this.name = name;
this.bd = bd;
this.age = function() {
2017 - this.bd;
}
}
var empName = document.getElementById("name").value;
var empBday = document.getElementById("bday").value;
var empList = new Employee(empName, empBday);
var Btn = document.getElementById('add').addEventListener('click', function() {
console.log(empList);
});
}
<input type="text" id="name" value=""></input>
<input type="text" id="bday" value=""></input>
<button id="add">Add</button>
那么即使我输入一些值,然后,我CONSOLE.LOG称为“emplist”,但我的目标只得到Employee {name: "", bd: "", age: ƒ}
当窗口负载的输入值都只是空字符串,那么这就是你所得到的。你期望别的吗? – adeneo
您可以在window.onload上设置值。您必须根据输入中的更改更新它们https://developer.mozilla.org/fi/docs/Web/Events/change 页面加载时它们为空。 – Rikusor
this.age需要返回一些东西,是不是? – JohnPan