admin管理员组文章数量:1321416
I have the following getter in one of my classes:
get password(){
if(this._public) return null;
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (var i = 0; i < 10; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
delete this.password;
return this.password = text;
}
There is no acpanying setter. On the line return this.password = text
I get this error:
Uncaught TypeError: Cannot set property password of # which has only a getter
However, the getter should be deleted at this point, and it should have no property called password. I actually took the idea from MDN's page here (the last code snippet on the page).
Anyone know why this is happening?
Tested on Chrome 51 and Node.js (v6.0.0).
I have the following getter in one of my classes:
get password(){
if(this._public) return null;
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (var i = 0; i < 10; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
delete this.password;
return this.password = text;
}
There is no acpanying setter. On the line return this.password = text
I get this error:
Uncaught TypeError: Cannot set property password of # which has only a getter
However, the getter should be deleted at this point, and it should have no property called password. I actually took the idea from MDN's page here (the last code snippet on the page).
Anyone know why this is happening?
Tested on Chrome 51 and Node.js (v6.0.0).
Share Improve this question asked Jul 2, 2016 at 0:01 JonahJonah 1,5454 gold badges17 silver badges34 bronze badges 8- Well, you can't set something if you have no setter, right? I think it's pretty obvious that you need a setter to actually set it – Darkrifts Commented Jul 2, 2016 at 0:05
- 2 @Darkrifts: This is not Ruby. A property can be either data property or accessor property. If I understand correctly, OP wants to delete the accessor property so he can put a data property in its place. – Amadan Commented Jul 2, 2016 at 0:09
- How is the password stored in your class? this._password? – skyline3000 Commented Jul 2, 2016 at 0:14
- 1 are you sure the error really es from the line you think it es from? I just tested the following on chrome: "var a = {get test(){delete this.test; return this.test = 123;} }; a.test" and it works. – Ronen Ness Commented Jul 2, 2016 at 0:15
-
3
@Ness: It seems to work for objects, but not for classes... jsfiddle/g11t1cns/1 Could it be because the property is not on the object, so there's nothing to delete? and then when you try to set, you're still looking up the proto chain and getting the
password
getter defined by the class... – Amadan Commented Jul 2, 2016 at 0:17
1 Answer
Reset to default 8As said in ments, you can't delete what isn't there. password
is not a property of user
, but of its prototype; so delete user.password
does nothing; if you then do user.password = "foo"
, you will find the property user
on the prototype, which is not settable.
Instead, you need to define a property on user
itself:
class User {
get password() {
Object.defineProperty(this, "password", {
value: "foo"
});
return this.password;
}
};
var user = new User();
console.log(user.password);
本文标签: Javascript won39t let me set property which only has a getter despite deleting getterStack Overflow
版权声明:本文标题:Javascript won't let me set property which only has a getter despite deleting getter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742024758a2415331.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论