使用VSCode尝试JS教程中的示例是JS的新手。当我设置对象属性时遇到此错误。第一个属性,例如firstname
,lastname
和age
看起来不错,但是当我尝试使用数组,函数或嵌套属性作为对象的属性时,出现错误,它将不会登录到控制台。
错误是:
“”。“(TS1005)
请告知,完全的菜鸟,但请努力学习更多,所以请原谅我的无知。
var person = {
firstname: 'Matilda',
lastname:'Milner',
age: 68
kids: ['Dave', 'Lynn', 'Cal', 'August', 'Cade'],
location: {
street: '500 Some St.',
city: 'Inola',
state: 'Oklahoma',
zip: '66702',
}
fullname: function(){
return this.firstname + " " + lastname;
}
}
console.log(person.firstname +" "+person.lastname + " " +"is"+ " " + person.age+ " "+ "years old");
console.log(person.kids[3])
console.log(person.address.zip)
答案 0 :(得分:1)
您的代码中几乎没有语法错误
,
。this
函数中的lastname
之前错过了fullname
address
的任何属性,但您正在尝试访问person.address.zip
var person = {
firstname: 'Matilda',
lastname:'Milner',
age: 68,
kids: ['Dave', 'Lynn', 'Cal', 'August', 'Cade'],
location: {
street: '500 Some St.',
city: 'Inola',
state: 'Oklahoma',
zip: '66702',
},
fullname: function(){
return this.firstname + " " + this.lastname;
}
}
console.log(person.firstname +" "+person.lastname + " " +"is"+ " " + person.age+ " "+ "years old");
console.log(person.kids[3])
console.log(person.location.zip)
console.log(person.fullname())