--- robots: noindex, nofollow tags: refactoring --- # Remove Setting Method > 練習用的 [REPL](https://repl.it/@yaosiang/RemoveSettingMethod) ```javascript= class Person { get name() {return this._name;} set name(arg) {this._name = arg;} // setter get id() {return this._id;} set id(arg) {this._id = arg;} // setter } ``` 呼叫端可以會直接指定 ID。 ```javascript= const martin = new Person(); martin.name = "martin"; martin.id = "1234"; // 使用了 setter ``` 但 ID 應該在物件建立後,就不允許修改。 ```javascript= class Person { constructor(id) { this.id = id; } get name() {return this._name;} set name(arg) {this._name = arg;} get id() {return this._id;} set id(arg) {this._id = arg;} } ``` 當可以使用建構子來傳遞 ID 後。 ```javascript= const martin = new Person("1234"); martin.name = "martin"; martin.id = "1234"; ``` ID 的 setter 就可以刪除了。 ```javascript= class Person { constructor(id) { this.id = id; } get name() {return this._name;} set name(arg) {this._name = arg;} get id() {return this._id;} // set id(arg) {this._id = arg;} // 移除 setter } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up