JavaScript 对象计划输出不匹配



代码的结尾是:

console.log ("Name: " + pete.fullname() + "tAge:" + pete.age);
console.log ("Name: " + cara.fullname() + "tAge:" + cara.age);
console.log (cara.firstname + "is" + (cara.age - pete.age) + " year older than " + pete.firstname);

我写的:

pete = {
firstname: "Pete",
lastname: "Programmer",
age: 20
};
var cara = {
firstname: "Cara",
lastname: "Coder",
age: 30,
};
pete.fullname = function (){
console.log (pete.firstname + pete.lastname)
};
cara.fullname = function () {
console.log (cara.firstname + 
cara.lastname)
};
console.log ("Name: " + pete.fullname() + "tAge:" + pete.age);
console.log ("Name: " + cara.fullname() + "tAge:" + cara.age);
console.log (cara.firstname + "is" + (cara.age - pete.age) + " year older than " + pete.firstname);

计划输出:

Name: Pete Programmer   Age:20 Name: 
Cara Coder  Age:32
Cara is 12 years older than Pete

不明白为什么我有这个输出:

PeteProgrammer
Name: undefined Age:20
CaraCoder
Name: undefined Age:30
Cara is 10 years older than Pete

pete.fullname()cara.fullname()函数中,执行的是console.log()而不是return。代码:

pete = {
firstname: "Pete",
lastname: "Programmer",
age: 20
};
var cara = {
firstname: "Cara",
lastname: "Coder",
age: 30,
};
pete.fullname = function (){
return pete.firstname + pete.lastname
}
cara.fullname = function () {
return cara.firstname + 
cara.lastname
};
console.log ("Name: " + pete.fullname() + "tAge:" + pete.age);
console.log ("Name: " + cara.fullname() + "tAge:" + cara.age);
console.log (cara.firstname + " is " + (cara.age - pete.age) + " year older than " + pete.firstname);

相关内容

  • 没有找到相关文章

最新更新