我试图创建一个属性的获取和设置方法:
private _name: string;
Name() {
get:
{
return this._name;
}
set:
{
this._name = ???;
}
}
设置值的关键字是什么?
我试图创建一个属性的获取和设置方法:
private _name: string;
Name() {
get:
{
return this._name;
}
set:
{
this._name = ???;
}
}
设置值的关键字是什么?
当前回答
它非常类似于创建常用方法,只需在开头放置关键字reserved get或set。
class Name{
private _name: string;
getMethod(): string{
return this._name;
}
setMethod(value: string){
this._name = value
}
get getMethod1(): string{
return this._name;
}
set setMethod1(value: string){
this._name = value
}
}
class HelloWorld {
public static main(){
let test = new Name();
test.setMethod('test.getMethod() --- need ()');
console.log(test.getMethod());
test.setMethod1 = 'test.getMethod1 --- no need (), and used = for set ';
console.log(test.getMethod1);
}
}
HelloWorld.main();
在这种情况下,你可以在get getMethod1(){中跳过返回类型
get getMethod1() {
return this._name;
}
其他回答
TS提供了getter和setter,使对象属性能够更好地控制如何在对象外部访问(getter)或更新(setter)。不是直接访问或更新属性,而是调用代理函数。
例子:
class Person {
constructor(name: string) {
this._name = name;
}
private _name: string;
get name() {
return this._name;
}
// first checks the length of the name and then updates the name.
set name(name: string) {
if (name.length > 10) {
throw new Error("Name has a max length of 10");
}
this._name = name;
}
doStuff () {
this._name = 'foofooooooofoooo';
}
}
const person = new Person('Willem');
// doesn't throw error, setter function not called within the object method when this._name is changed
person.doStuff();
// throws error because setter is called and name is longer than 10 characters
person.name = 'barbarbarbarbarbar';
下面是一个工作示例,可以为你指明正确的方向:
class Foo {
_name;
get Name() {
return this._name;
}
set Name(val) {
this._name = val;
}
}
JavaScript中的getter和setter只是普通的函数。setter是一个接受参数的函数,该参数的值就是被设置的值。
尽管TypeScript会分析属性的初始化,但如果你总是想自己处理这种情况,你可以在ts.config.json中将此设置设置为false。
{
"compilerOptions": {
"strict": true,
"strictPropertyInitialization": false
}
}
严格属性初始化- strictPropertyInitialization设置时 为true时,当类属性为时,TypeScript将引发错误 在构造函数中声明但未设置。
在这种情况下,您还应该考虑其他情况,您将在下面的链接中看到。
class UserAccount {
name: string;
accountType = "user";
email: string;//Property 'email' has no initializer and is not definitely assigned in the constructor.
address: string | undefined;
constructor(name: string) {
this.name = name;
// Note that this.email is not set
}
}
This.name是专门设置的。 这一点。默认设置为accountType。 这一点。未设置电子邮件并引发错误。 这一点。地址声明为潜在未定义,这意味着它不需要设置。
如果我们将strictPropertyInitialization设置为false,编译器不会引发错误
private _name : string;
public get name() : string {
return this._name;
}
public set name(v : string) {
this._name = v;
}
https://www.typescriptlang.org/docs/handbook/2/classes.html#--strictpropertyinitialization https://www.typescriptlang.org/tsconfig#strictPropertyInitialization
下面是如何添加getter和setter的示例
class Person {
private _age: number;
private _firstName: string;
private _lastName: string;
public get age() {
return this._age;
}
public set age(theAge: number) {
if (theAge <= 0 || theAge >= 200) {
throw new Error('The age is invalid');
}
this._age = theAge;
}
public getFullName(): string {
return `${this._firstName} ${this._lastName}`;
}
}
你可以这样写
class Human {
private firstName : string;
private lastName : string;
constructor (
public FirstName?:string,
public LastName?:string) {
}
get FirstName() : string {
console.log("Get FirstName : ", this.firstName);
return this.firstName;
}
set FirstName(value : string) {
console.log("Set FirstName : ", value);
this.firstName = value;
}
get LastName() : string {
console.log("Get LastName : ", this.lastName);
return this.lastName;
}
set LastName(value : string) {
console.log("Set LastName : ", value);
this.lastName = value;
}
}