代码的结尾是:
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);