我试图创建一个属性的获取和设置方法:
private _name: string;
Name() {
get:
{
return this._name;
}
set:
{
this._name = ???;
}
}
设置值的关键字是什么?
我试图创建一个属性的获取和设置方法:
private _name: string;
Name() {
get:
{
return this._name;
}
set:
{
this._name = ???;
}
}
设置值的关键字是什么?
当前回答
根据您所展示的示例,您希望通过get()传递一个数据对象并获取该对象的属性。为此需要使用泛型类型,因为数据对象是泛型的,可以是任何对象。
export class Attributes<T> {
constructor(private data: T) {}
get = <K extends keyof T>(key: K): T[K] => {
return this.data[key];
};
set = (update: T): void => {
// this is like spread operator. it will take this.data obj and will overwrite with the update obj
// ins tsconfig.json change target to Es6 to be able to use Object.assign()
Object.assign(this.data, update);
};
getAll(): T {
return this.data;
}
}
< T >指泛型类型。让我们初始化一个实例
const myAttributes=new Attributes({name:"something",age:32})
myAttributes.get("name")="something"
注意这个语法
<K extends keyof T>
为了能够使用这个,我们应该注意两件事:
1- in typestring字符串可以是一个类型。
2- javascript中所有对象属性本质上都是字符串。
当我们使用get()时,它接收的参数类型是传递给构造函数的对象属性,由于对象属性是字符串,并且字符串在typescript中允许是类型,我们可以使用这个<K extends keyof T>
其他回答
Ezward已经提供了一个很好的答案,但我注意到其中一个评论问它是如何使用的。对于像我这样偶然遇到这个问题的人,我认为在Typescript网站上有一个关于getter和setter的官方文档的链接会很有用,因为它能很好地解释它,并希望在发生更改时始终保持最新,并显示示例用法:
http://www.typescriptlang.org/docs/handbook/classes.html
特别地,对于那些不熟悉它的人,请注意,在调用getter时不要合并'get'这个词(setter也是如此):
var myBar = myFoo.getBar(); // wrong
var myBar = myFoo.get('bar'); // wrong
你应该简单地这样做:
var myBar = myFoo.bar; // correct (get)
myFoo.bar = true; // correct (set) (false is correct too obviously!)
给定一个类:
class foo {
private _bar:boolean = false;
get bar():boolean {
return this._bar;
}
set bar(theBar:boolean) {
this._bar = theBar;
}
}
然后私有属性'_bar'的'bar' getter将被调用。
尽管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
下面是一个工作示例,可以为你指明正确的方向:
class Foo {
_name;
get Name() {
return this._name;
}
set Name(val) {
this._name = val;
}
}
JavaScript中的getter和setter只是普通的函数。setter是一个接受参数的函数,该参数的值就是被设置的值。
我想我大概知道为什么这么令人困惑了。在您的示例中,我们需要_name的getter和setter。但我们通过为不相关的类变量Name创建getter和setter来实现这一点。
考虑一下:
class Car {
private tiresCount = 4;
get yourCarTiresCount(){
return this.tiresCount;
}
set yourCarTiresCount(count) {
alert('You shouldn\'t change car tire count')
}
}
上面的代码如下:
get和set为yourCarTiresCount创建getter和setter(不是为tiresCount)。
getter是:
function () {
return this.tiresCount;
}
setter是:
function (count) {
alert('You shouldn\'t change car tire count');
}
意思是,每次执行new Car()。yourCarTiresCount, getter运行。并且对于每一个新的Car(). yourcartirescount ('7') setter运行。
间接地为私有ti复述创建getter,而不是setter。
根据您所展示的示例,您希望通过get()传递一个数据对象并获取该对象的属性。为此需要使用泛型类型,因为数据对象是泛型的,可以是任何对象。
export class Attributes<T> {
constructor(private data: T) {}
get = <K extends keyof T>(key: K): T[K] => {
return this.data[key];
};
set = (update: T): void => {
// this is like spread operator. it will take this.data obj and will overwrite with the update obj
// ins tsconfig.json change target to Es6 to be able to use Object.assign()
Object.assign(this.data, update);
};
getAll(): T {
return this.data;
}
}
< T >指泛型类型。让我们初始化一个实例
const myAttributes=new Attributes({name:"something",age:32})
myAttributes.get("name")="something"
注意这个语法
<K extends keyof T>
为了能够使用这个,我们应该注意两件事:
1- in typestring字符串可以是一个类型。
2- javascript中所有对象属性本质上都是字符串。
当我们使用get()时,它接收的参数类型是传递给构造函数的对象属性,由于对象属性是字符串,并且字符串在typescript中允许是类型,我们可以使用这个<K extends keyof T>