所以我正在关注codecademy的javascript课程,我决定在我自己的脚本上尝试一下。所以脚本是:
function family1 (name, age) {
this.name = name;
this.age = age;
}
var family = new Array();
family[0] = new family1("alice", 40);
family[1] = new family1("bob", 42);
family[2] = new family1("michelle", 8);
family[3] = new family1("timmy", 8);
printFamily1 = function(person) {
console.log(person.name + " aged " + person.age);
}
console.log(printFamily1(family[0]))
console.log(printFamily1(family[1]))
console.log(printFamily1(family[2]))
console.log(printFamily1(family[3]))
它有效,但在控制台中,它打印
alice aged 40
undefined
bob aged 42
undefined
michelle aged 8
undefined
timmy aged 8
undefined
它有效,但我不想要未定义的部分!我该如何解决这些问题!
答案 0 :(得分:0)
将printFamily1函数更新为:
printFamily1 = function(person) {
console.log(person.name + " aged " + person.age);
return (person.name + " aged " + person.age);
}
问题是您尝试打印printFamily1
返回的内容,但您没有定义返回值。因此,只需在控制台中添加要打印的内容
答案 1 :(得分:0)
您有两个选择:
请勿再次致电{"epn":{"events":[{"name":"STCSensor","createdDate":"Mon Oct 12 2015","attributes":[
{"name":"entityType","type":"String","dimension":0},
{"name":"Serial","type":"Double","dimension":0},
{"name":"BatteryLevel","type":"Integer","dimension":0},
{"name":"CurrentRSSI","type":"Integer","dimension":0},
{"name":"LastPacketRSSI","type":"Integer","dimension":0},
{"name":"LastPacketSNR","type":"Integer","dimension":0},
{"name":"timestamp","type":"Integer","dimension":0},
{"name":"timestampDelta","type":"Integer","dimension":0},
{"name":"timestampDelta2","type":"Integer","dimension":0},
{"name":"Latitude","type":"Double","dimension":0},
{"name":"Longitude","type":"Double","dimension":0},
{"name":"Altitude","type":"Integer","dimension":0},
{"name":"Speed","type":"Integer","dimension":0},
{"name":"LatitudeDelta1","type":"Integer","dimension":0},
{"name":"LongitudeDelta1","type":"Integer","dimension":0},
{"name":"AltitudeDelta1","type":"Integer","dimension":0},
{"name":"SpeedDelta1","type":"Integer","dimension":0},
{"name":"LatitudeDelta2","type":"Integer","dimension":0},
{"name":"LongitudeDelta2","type":"Integer","dimension":0},
{"name":"AltitudeDelta2","type":"Integer","dimension":0},
{"name":"SpeedDelta2","type":"Integer","dimension":0},
{"name":"temperature","type":"Integer","dimension":0},
{"name":"pressure","type":"Integer","dimension":0},
{"name":"humidity","type":"Integer","dimension":0},
{"name":"temperatureCal","type":"Double","dimension":0},
{"name":"pressureCal","type":"Double","dimension":0},
{"name":"humidityCal","type":"Double","dimension":0},
{"name":"CO","type":"Integer","dimension":0},
{"name":"NO","type":"Integer","dimension":0},
{"name":"NO2","type":"Integer","dimension":0},
{"name":"Ozone","type":"Integer","dimension":0},
{"name":"COCal","type":"Double","dimension":0},
{"name":"NOCal","type":"Double","dimension":0},
{"name":"NO2Cal","type":"Double","dimension":0},
{"name":"OzoneCal","type":"Double","dimension":0},
{"name":"entityId","type":"Integer","dimension":"0"}]},
:
console.log
请勿在函数
中调用printFamily1(family[0])
printFamily1(family[1])
printFamily1(family[2])
printFamily1(family[3])
console.log