【发布时间】:2015-06-24 15:49:44
【问题描述】:
我正在使用 TypeScript 定义一些类,当我创建一个属性时,它会在以下 plunkr 中生成与 Class1 等效的内容:
http://plnkr.co/edit/NXUo7zjJZaUuyv54TD9i?p=preview
var Class1 = function () {
this._name = "test1";
}
Object.defineProperty(Class1.prototype, "Name", {
get: function() { return this._name; },
set: function(value) { this._name = value; },
enumerable: true
});
JSON.stringify(new Class1()); // Will be "{"_name":"test1"}"
序列化的时候,并没有输出我刚才定义的属性。
instance2 和 instance3 通过序列化定义的属性,表现得和我预期的一样。 (参见 plunkr 输出)。
我的实际问题是:这正常吗?
如果是这样,我该如何以最有效的方式解决它?
【问题讨论】:
标签: javascript json typescript