var group = new Konva.Group();
group.customProp = ['a', 'b', 'c'];
group.toJSON();
结果:
{
attrs: {},
className: "Group",
children: []
}
customProp
丢失了。
如何在不丢失数据的情况下将对象转换为JSON?
答案 0 :(得分:1)
实例上的属性不会被序列化。您应该使用公共API来设置/获取属性。
var group = new Konva.Group();
group.setAttr('customProp', [1, 2, 3]);
// use this to read property
// group.getAttr('customProp')
group.toJSON();
输出:
{"attrs":{"customProp":[1,2,3]},"className":"Group","children":[]}
答案 1 :(得分:0)
var group = new Konva.Group();
Object.defineProperty(group," customProp",[' a''''' c'])
group.toJSON();
//看这里 // https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty