将javascript类实例转换为普通对象保留方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了将javascript类实例转换为普通对象保留方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2853字,纯文字阅读大概需要5分钟。
内容图文
![将javascript类实例转换为普通对象保留方法](/upload/InfoBanner/zyjiaocheng/788/ad2158b54b3247b1a95fd44d1650fd2e.jpg)
我想将实例类转换为普通对象,而不会丢失方法和/或继承的属性.例如:
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();
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'); }
}
这有可能实现,如果是,如何实现?
如果你想知道,我需要这个,因为我使用的框架不幸地接受了输入
只有一个json对象,而我正在尝试使用typescript和类继承.
此外,我正在进行上述转换,因此性能不是一个需要考虑的问题.
UPDATE
如果编译器的目标选项设置为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;
}
但这仍然无效.结果对象仅包含所有类的所有属性,但仅包含PersonWorker中的方法.
我在这里错过了什么?
解决方法:
好吧,所以我的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;
}
内容总结
以上是互联网集市为您收集整理的将javascript类实例转换为普通对象保留方法全部内容,希望文章能够帮你解决将javascript类实例转换为普通对象保留方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。