我正在尝试此代码
var mark = new Employee;
mark.name = 'Doe, Mark';
mark.dept = 'admin';
mark.projects = ['navigator'];
console.log(mark);
但是当我尝试打印时,控制台不会显示任何内容。它仅在我使用新对象时显示。有什么问题?
答案 0 :(得分:0)
您需要将Employee
定义为构造函数:
function Employee() {}
var mark = new Employee();
mark.name = 'Doe, Mark';
mark.dept = 'admin';
mark.projects = ['navigator'];
console.log(mark);

这也允许您使用如下构造函数设置属性:
function Employee(name){
this.name = name
}
var mark = new Employee('Doe, Mark');
mark.dept = 'admin';
mark.projects = ['navigator'];
console.log(mark);