eAb*_*Abi 5 javascript prototypal-inheritance typescript ecmascript-6
我想将实例类转换为普通对象,而又不会丢失方法和/或继承的属性。因此,例如:
class Human {
height: number;
weight: number;
constructor() {
this.height = 180;
this.weight = 180;
}
getWeight() { return this.weight; }
// I want this function to convert the child instance
// accordingly
toJSON() {
// ???
return {};
}
}
class Person extends Human {
public name: string;
constructor() {
super();
this.name = 'Doe';
}
public getName() {
return this.name;
}
}
class PersonWorker extends Person {
constructor() {
super();
}
public report() {
console.log('I am Working');
}
public test() {
console.log('something');
}
}
let p = new PersonWorker;
let jsoned = p.toJSON();
Run Code Online (Sandbox Code Playgroud)
jsoned 应该看起来像这样:
{
// from Human class
height: 180,
weight: 180,
// when called should return this object's value of weight property
getWeight: function() {return this.weight},
// from Person class
name: 'Doe'
getName(): function() {return this.name},
// and from PersonWorker class
report: function() { console.log('I am Working'); },
test: function() { console.log('something'); }
}
Run Code Online (Sandbox Code Playgroud)
这有可能实现吗?如果可以,如何实现?
如果您想知道,我需要这个,因为不幸的是,我正在使用一个仅接受json对象作为输入的框架,而我却尝试使用typescriptclass继承。
另外,我一次进行了上述转换,因此性能不是要考虑的问题。
如果将编译器的target选项设置为,则由迭代对象属性组成的解决方案将不起作用es6。在上es5,通过迭代对象属性(使用Object.keys(instance))的现有实现将起作用。
到目前为止,我已经实现了:
toJSON(proto?: any) {
// ???
let jsoned: any = {};
let toConvert = <any>proto || this;
Object.getOwnPropertyNames(toConvert).forEach((prop) => {
const val = toConvert[prop];
// don't include those
if (prop === 'toJSON' || prop === 'constructor') {
return;
}
if (typeof val === 'function') {
jsoned[prop] = val.bind(this);
return;
}
jsoned[prop] = val;
const proto = Object.getPrototypeOf(toConvert);
if (proto !== null) {
Object.keys(this.toJSON(proto)).forEach(key => {
if (!!jsoned[key] || key === 'constructor' || key === 'toJSON') return;
if (typeof proto[key] === 'function') {
jsoned[key] = proto[key].bind(this);
return;
}
jsoned[key] = proto[key];
});
}
});
return jsoned;
}
Run Code Online (Sandbox Code Playgroud)
但这仍然行不通。结果对象仅包括所有类的所有属性,而仅包括中的方法PersonWorker。我在这里想念什么?
这对我有用
const classToObject = theClass => {
const originalClass = theClass || {}
const keys = Object.getOwnPropertyNames(Object.getPrototypeOf(originalClass))
return keys.reduce((classAsObj, key) => {
classAsObj[key] = originalClass[key]
return classAsObj
}, {})
}
Run Code Online (Sandbox Code Playgroud)
小智 7
已经有很多答案了,但这是最简单的,通过使用 ... 运算符并解构对象:
const {...object} = classInstance
Run Code Online (Sandbox Code Playgroud)
好的,所以我的 OP 中的实现是错误的,这个错误简直是愚蠢的。
使用时的正确实现es6是:
toJSON(proto) {
let jsoned = {};
let toConvert = proto || this;
Object.getOwnPropertyNames(toConvert).forEach((prop) => {
const val = toConvert[prop];
// don't include those
if (prop === 'toJSON' || prop === 'constructor') {
return;
}
if (typeof val === 'function') {
jsoned[prop] = val.bind(jsoned);
return;
}
jsoned[prop] = val;
});
const inherited = Object.getPrototypeOf(toConvert);
if (inherited !== null) {
Object.keys(this.toJSON(inherited)).forEach(key => {
if (!!jsoned[key] || key === 'constructor' || key === 'toJSON')
return;
if (typeof inherited[key] === 'function') {
jsoned[key] = inherited[key].bind(jsoned);
return;
}
jsoned[key] = inherited[key];
});
}
return jsoned;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10656 次 |
| 最近记录: |