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
 |  Show 3 more ments

1 Answer 1

Reset to default 8

As 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