简体   繁体   English

Javascript控制台说:左侧分配无效?

[英]Javascript console says: Invalid left-side assignment?

So, this is to make a secure login. 因此,这是进行安全的登录。 i run this through chrome's javascript console and i get 'Uncaught ReferenceError: Invalid left-hand side in assignment '?! 我通过chrome的javascript控制台运行此命令,并收到“ Uncaught ReferenceError:分配中的无效左侧”?! please help! 请帮忙!

function logIn() {
String.prototype.hashCode = function() {
        for(var ret = 0, i = 0, len = this.length; i < len; i++) {
        ret = (31 * ret + this.charCodeAt(i)) << 0;
    }
        return ret;
    };

var user = document.getElementById("username"),
    pass = document.getElementById("password");
if ((user = "Fedora") && (pass.hashCode() = -976887139)) {
    window.alert("Captain Fedora: SuperUser");

}
}   
if ((user = "Fedora") && (pass.hashCode() = -976887139))

should be 应该

if ((user == "Fedora") && (pass.hashCode() == -976887139))

so you do comparison, and not assignment 所以你做比较,而不是分配

Yes, pass.hashCode() = -976887139 is an assignment . 是的, pass.hashCode() = -976887139是一个赋值 And since the function call doesn't return a reference , it's invalid to assign to it. 而且由于函数调用未返回引用 ,因此将其分配给它是无效的。 You probably wanted the comparison 您可能想要比较

pass.hashCode() == -976887139

Also, you will want to get the value s of those DOM elements (I can't believe the password input has a hashCode method at all), and you also want to compare the content of the user variable with that string instead of assigning them. 另外,您将想要获取那些DOM元素的value s (我不敢相信密码输入完全具有hashCode方法),并且您还想将user变量的内容与该字符串进行比较,而不是分配它们。

var user = document.getElementById("username").value,
    pass = document.getElementById("password").value;
if (user == "Fedora" && pass.hashCode() == -976887139) {
    window.alert("Captain Fedora: SuperUser");
}
if ((user = "Fedora") && (pass.hashCode() = -976887139))

change to 改成

if ((user == "Fedora") && (pass.hashCode() == -976887139))

also 'reuturn ret' may return undefined because in wrong scope. “ reuturn ret”也可能返回未定义,因为范围错误。 var ret = 0 only work in 'for' scope var ret = 0仅在“ for”范围内起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM