Javascript 如何在TypeScript中创建值属性
TypeScript有一个用于定义访问器属性的内置功能Javascript 如何在TypeScript中创建值属性,javascript,typescript,Javascript,Typescript,TypeScript有一个用于定义访问器属性的内置功能 class Test { constructor(private value: number = 123) { } public get Value(): number { return this.value; } public set Value(value: number) { this.value = value; } } Object.define
class Test {
constructor(private value: number = 123) {
}
public get Value(): number {
return this.value;
}
public set Value(value: number) {
this.value = value;
}
}
Object.defineProperty(someObj, "MyValueProperty", {
// The key here is value as opposed to get and set.
value: 5
enumerable: true,
configurable: false
});
编译器输出
var Test = (function () {
function Test(value) {
if (value === void 0) { value = 123; }
this.value = value;
}
Object.defineProperty(Test.prototype, "Value", {
get: function () {
return this.value;
},
set: function (value) {
this.value = value;
},
enumerable: true,
configurable: true
});
return Test;
})();
JavaScript还支持值属性
class Test {
constructor(private value: number = 123) {
}
public get Value(): number {
return this.value;
}
public set Value(value: number) {
this.value = value;
}
}
Object.defineProperty(someObj, "MyValueProperty", {
// The key here is value as opposed to get and set.
value: 5
enumerable: true,
configurable: false
});
如何使用TypeScript定义值属性
注意:我注意到我正被指向另一个关于打字脚本获取器和设置器的stackoverflow问题。这不是我想要的。我想知道如何创建实现
值
、非获取
和设置
的属性 Typescript目前不支持该功能。查看语言规范和typescript编译器的发射器类,我找不到任何表示支持值属性的内容
我想您唯一的选择是使用原始Javascript和对象.defineProperty
来定义它们。Typescript目前不支持这一点。查看语言规范和typescript编译器的发射器类,我找不到任何表示支持值属性的内容
我想您唯一的选择是使用原始Javascript和Object.defineProperty
来定义它们。您可以执行以下操作:
class Test {
Value : number;
}
Object.defineProperty(Test.prototype, "Value", {
value: 5,
enumerable: true,
configurable: false
});
var t = new Test();
t.Value = 54;
console.log(t.Value); // 5
但是为什么不在get函数中返回值呢
class Test {
public get Value(): number {
return 5;
}
}
var t = new Test();
t.Value = 54;
console.log(t.Value); // 5
使用装饰器
如果您真的想要值属性,一种更优雅的方法是创建一个可重用的装饰器:
function ValueProperty(value: any) {
return (target: Object, propertyKey: string) => {
Object.defineProperty(target, propertyKey, {
value,
enumerable: true,
configurable: false
});
};
}
然后使用它:
class Test {
@ValueProperty(5)
Value: number;
}
new Test().Value; // 5
您可以执行以下操作:
class Test {
Value : number;
}
Object.defineProperty(Test.prototype, "Value", {
value: 5,
enumerable: true,
configurable: false
});
var t = new Test();
t.Value = 54;
console.log(t.Value); // 5
但是为什么不在get函数中返回值呢
class Test {
public get Value(): number {
return 5;
}
}
var t = new Test();
t.Value = 54;
console.log(t.Value); // 5
使用装饰器
如果您真的想要值属性,一种更优雅的方法是创建一个可重用的装饰器:
function ValueProperty(value: any) {
return (target: Object, propertyKey: string) => {
Object.defineProperty(target, propertyKey, {
value,
enumerable: true,
configurable: false
});
};
}
然后使用它:
class Test {
@ValueProperty(5)
Value: number;
}
new Test().Value; // 5
请让你的标题更有意义。@Martin-我已经演示了使用getter和setter的能力。请看我的通知。我在你的链接中没有看到任何关于价值实现的内容。请让你的标题更有意义。@Martin-我已经演示了使用getter和setter的能力。请看我的通知。我在你的链接中没有看到任何关于价值实现的内容