无法读取 javascript 中未定义的 属性 'set'
Cannot read property 'set' of undefined in javascript
我正在使用 fabric 3.4
创建一个 canvas 元素,该元素提供 klass
对象。
说,我有变量 a
as
a = klass {lockScalingX: true}
我正在将其克隆到 b
并使用一些字典对象更新 b
b = a.clone();
b.set({lockMovementY: true});
但这会报错,
index.html:3 ERROR TypeError: Cannot read property 'set' of undefined
当我控制台记录 a
和 b
时。克隆后的b是undefined
.
如何克隆 klass
对象并使用 set()
更新值?
或者把 fabric 的 klass
改成简单的对象?
Edit 2
这里是控制台日志的截图。
这是一个数组,控制台有按键 8、9、10 打印。我已扩展 8
以显示密钥中包含的值。
b[s] = a[s].clone();
console.log('a: ', a); // This consoles the first code in the question.
console.log('b[s]: ', b[s]); // This prints undefined
b[s].set({lockMovementY: true}); // This is giving error. Cannot read property set of undefined.
我调用fabric的toObject()
方法解决了
之前我使用的是 fabric 1.x 并且正在进行项目改造。将结构升级到 3.x 后,我开始面临这个问题。创建结构 canvas 时,返回类型 klass
的对象。
const canvas = new fabric.Canvas();
为了将 klass
转换为对象,我使用了 toObject()
方法,例如
canvas.toObject()
哪个 returns 简单对象而不是 klass
对象。
我正在使用 fabric 3.4
创建一个 canvas 元素,该元素提供 klass
对象。
说,我有变量 a
as
a = klass {lockScalingX: true}
我正在将其克隆到 b
并使用一些字典对象更新 b
b = a.clone();
b.set({lockMovementY: true});
但这会报错,
index.html:3 ERROR TypeError: Cannot read property 'set' of undefined
当我控制台记录 a
和 b
时。克隆后的b是undefined
.
如何克隆 klass
对象并使用 set()
更新值?
或者把 fabric 的 klass
改成简单的对象?
Edit 2
这里是控制台日志的截图。
这是一个数组,控制台有按键 8、9、10 打印。我已扩展 8
以显示密钥中包含的值。
b[s] = a[s].clone();
console.log('a: ', a); // This consoles the first code in the question.
console.log('b[s]: ', b[s]); // This prints undefined
b[s].set({lockMovementY: true}); // This is giving error. Cannot read property set of undefined.
我调用fabric的toObject()
方法解决了
之前我使用的是 fabric 1.x 并且正在进行项目改造。将结构升级到 3.x 后,我开始面临这个问题。创建结构 canvas 时,返回类型 klass
的对象。
const canvas = new fabric.Canvas();
为了将 klass
转换为对象,我使用了 toObject()
方法,例如
canvas.toObject()
哪个 returns 简单对象而不是 klass
对象。