【问题标题】:Why won't this object property print? (Typo) [closed]为什么不打印此对象属性? (错字)[关闭]
【发布时间】:2016-07-31 16:37:40
【问题描述】:
function User()
{
    this.username = null;
    this.password = null;
    this.first = null;
    this.last = null;
}

var userList = new Array();

userList[0] = new User();
userList[1] = new User();
userList[2] = new User();

userList[0].username = "Georgie19";
userList[1].username = "PaulC-87";
uesrList[2].username = "JamesHat";

userList[0].password = "georgepass101";
userList[1].password = "adminpaul-13";
userList[2].password = "jamesH7";

userList[0].first = "George";
userList[1].first = "Paul";
userList[2].first = "James";

userList[0].last = "Hordin";
userList[1].last = "Corman";
userList[2].last = "Haquel";

document.write(userList[0].username);

当我将其放入浏览器时,它显示为空白。我已经尝试摆脱我不使用的东西,它会自动工作。我实在想不通。我怀疑这是我根本找不到的语法错误,请帮忙。 PS:这不是我的 HTML 的问题

【问题讨论】:

  • “我已经尝试摆脱我不使用的东西”,哪些东西?
  • uesrList 真的是您在控制台中尝试过的吗? (第一个块分配给.usernameuesrList[2].username = ...
  • 此外,不确定您的应用程序到底是什么,但要知道,在 JavaScript 中以纯文本形式存储密码,就安全性而言,相当于告诉用户每个人的密码是什么。
  • 仅供参考,没有必要重复所有这些:pastie.org/10792528 是的,@JCOC611 所说的。大时代。

标签: javascript html web


【解决方案1】:

userList 这一行的拼写错误会导致脚本错误,从而导致您的代码无法运行:

uesrList[2].username = "JamesHat";

请学会在调试控制台中查看您自己的脚本错误,并在此处清楚地说明这一错误。

修复该错误后,您的脚本运行良好。

工作sn-p:

function User()
{
    this.username = null;
    this.password = null;
    this.first = null;
    this.last = null;
}

var userList = new Array();

userList[0] = new User();
userList[1] = new User();
userList[2] = new User();

userList[0].username = "Georgie19";
userList[1].username = "PaulC-87";
userList[2].username = "JamesHat";

userList[0].password = "georgepass101";
userList[1].password = "adminpaul-13";
userList[2].password = "jamesH7";

userList[0].first = "George";
userList[1].first = "Paul";
userList[2].first = "James";

userList[0].last = "Hordin";
userList[1].last = "Corman";
userList[2].last = "Haquel";

document.write(userList[0].username);

【讨论】:

  • 通过说“我本可以在控制台中查看”,这是什么意思?什么控制台?我只是在 Windows 中将其作为文件运行,它会出现在浏览器中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-30
  • 2011-12-11
  • 2014-11-23
  • 2018-11-03
  • 1970-01-01
  • 2020-08-28
  • 1970-01-01
相关资源
最近更新 更多