更新时间:2023-10-29 21:15:40
你的问题在于在构造实例时没有声明name
,因为根本没有为构造函数传参数,那么真是如此吗?
function test(t) {
console.log(t);//undefined
console.log(a);//报错
}
test();
可以看到,即使没有给一个带参的函数传参,也能访问到这个参数,这个参数的值是undefined
。
这其实涉及到arguments
,js函数定义时的参数其实是不必要的,因为函数内部其实访问的是一个类数组对象arguments
,传进来的参数依次放到这个对象中。
function test(t) {
console.log(arguments[0]);//undefined
console.log(arguments[1]);//undefined
}
test();
所以第一个构造函数没有声明和初始化name
,实例去原型中找name属性。但剩下的两个构造函数为name
赋值undefined
。
所以这道题的答案是:Tomundefinedjohn
。