Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Setter未设置其他属性值_Javascript - Fatal编程技术网

Javascript Setter未设置其他属性值

Javascript Setter未设置其他属性值,javascript,Javascript,我正在使用Javascript getter和setter,似乎无法从setter内部设置附加属性的值: 'use strict'; var lastUserId = 0; function getNewUserId() { var id = lastUserId; lastUserId++; return id; } function User(_name, _email, _password) { this.Name = _name; this.

我正在使用Javascript getter和setter,似乎无法从setter内部设置附加属性的值:

'use strict';

var lastUserId = 0;

function getNewUserId()
{
    var id = lastUserId;
    lastUserId++;
    return id;
}

function User(_name, _email, _password)
{
    this.Name = _name;
    this.Email = _email;
    this.Id = getNewUserId();

    //Make the Id read-only
    Object.defineProperty(this, 'Id', {writable: false});

    //Test changing the Id - Should get a read-only error.
    //this.Id = 12;

    this.PasswordHash = -1; //Inital value
    var PasswordValue;


    Object.defineProperty(User, "Password",
    {
        configurable: true,
        get: function() {
            return this.PasswordValue;
        },
        set : function(value) {
            this.PasswordHash = hashCode(value);
            this.PasswordValue = value;
        }
    });

    this.Password = _password;
    //this.PasswordHash = "thisWorks";
}

function hashCode (val) {
    var hash = 0, i, chr, len;
    if (val.length === 0) return hash;
    for (i = 0, len = val.length; i < len; i++) {
        chr   = val.charCodeAt(i);
        hash  = ((hash << 5) - hash) + chr;
        hash |= 0; // Convert to 32bit integer
    }
    return hash;
};
“严格使用”;
var lastUserId=0;
函数getNewUserId()
{
var id=lastUserId;
lastUserId++;
返回id;
}
功能用户(\u名称、\u电子邮件、\u密码)
{
this.Name=\u Name;
this.Email=\u Email;
this.Id=getNewUserId();
//将Id设置为只读
defineProperty(这个'Id',{writeable:false});
//更改Id的测试-应获得只读错误。
//这个。Id=12;
this.PasswordHash=-1;//初始值
var密码值;
Object.defineProperty(用户,“密码”,
{
对,,
get:function(){
返回this.PasswordValue;
},
设置:函数(值){
this.PasswordHash=hashCode(值);
this.PasswordValue=value;
}
});
this.Password=\u Password;
//this.PasswordHash=“thisWorks”;
}
函数hashCode(val){
var hash=0,i,chr,len;
如果(val.length==0)返回哈希;
对于(i=0,len=val.length;ihash=((hash你需要在
对象内部调用
这个
。定义属性(用户,“密码”,
。我对你的代码做了一点修改,它现在可以工作了

“严格使用”;
var lastUserId=0;
函数getNewUserId()
{
var id=lastUserId;
lastUserId++;
返回id;
}
var User=函数(\u名称、\u电子邮件、\u密码){
this.Name=\u Name;
this.Email=\u Email;
this.Id=getNewUserId();
//将Id设置为只读
defineProperty(这个'Id',{writeable:false});
Object.defineProperty(这是“密码”{
对,,
get:function(){
返回this.PasswordValue;
},
设置:函数(值){
this.PasswordHash=hashCode(值);
this.PasswordValue=value;
}
});
this.Password=\u Password;
}
函数hashCode(val){
var hash=0,i,chr,len;
如果(val.length==0)返回哈希;
对于(i=0,len=val.length;ihash=((hash@AndrewLi-我正在构造函数内部(右端)将
Password
设置为
\u Password
参数的值。Ahh,因此我将实例而不是类型传递给
对象。defineProperty
…我本来可以(而且几乎做到了)为了这个,我花了很多时间才把头撞在墙上!呵呵,Javascript对象属性有时会很棘手,我还有什么可以帮你解决这个问题的吗?没有,这已经解决了所有问题(现在)谢谢!)