我对象的方法显示的是未定义在输出端

问题描述 投票:-2回答:1
var people = {
  doctor: "M.D",
  specialization: "Neurologist",
  city: "NY",
  patient: "A.H",
  print: function() {
    document.write("People Object" + "<br>");
    document.write(this.doctor + "<br>");
    document.write(this.specialization + "<br>");
    document.write(this.city + "<br>");
    document.write(this.patient + "<br>");
  }
}
document.write(people.print());
javascript object methods
1个回答
0
投票

function返回的东西。如果你没有在你的函数return语句,它将返回undefined

当您执行功能people.print()它返回undefined,而你试图写它document,这是你想达到什么不正确的方法。

当你的function已经写在document文字,你不需要换包装内functiondocument.write执行。

如果您需要在JavaScript中,read this功能的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.