简体   繁体   English

未捕获的类型错误:无法读取未定义的属性“hasOwnProperty”

[英]Uncaught TypeError: Cannot read property 'hasOwnProperty' of undefined

I'm using fabric.js to draw some text on a canvas.我正在使用 fabric.js 在画布上绘制一些文本。 I have a function to create a text label.我有一个创建文本标签的功能。 I'd like to have labels run a function when they're selected.我想让标签在被选中时运行一个功能。 The syntax for this is label.on('selected', functionToCall());其语法是label.on('selected', functionToCall()); This works fine when I make the function an anonymous inner function, but when I break it out as a separate function, I get an Uncaught TypeError:当我使函数成为匿名内部函数时,这很好用,但是当我将它作为一个单独的函数分解时,我得到一个未捕获的类型错误:

Cannot read property 'hasOwnProperty' of undefined.无法读取未定义的属性“hasOwnProperty”。

What am I doing wrong?我究竟做错了什么?

Below is the code which doesn't work for me.下面是对我不起作用的代码。 Here's the broken code on jsfiddle, and a version set up with an anonymous function which works.这是jsfiddle上的损坏代码,以及一个使用匿名函数设置的版本。

"use strict";

var canvas = new fabric.Canvas('c', {selection: false}),
  position = 50;

function onLabelSelection(theLabel) {
  if (theLabel.hasOwnProperty('edge')) {
    selectedEdge = theLabel.edge;
  } else {
    selectedEdge = null;
  }
}

function createLabel() {
  var label;
    
  label = new fabric.Text('Hello World', {
    left: position,
    top: position
  });
  position += 50;
    
  label.edge = null;

  label.on('selected', onLabelSelection(this));

  return label;
}

canvas.on('mouse:up', function() {
  var newLabel = createLabel(); 
  canvas.add(newLabel);
});

The syntax for this is label.on('selected', functionToCall())语法是label.on('selected', functionToCall())

No. The syntax for event handlers is to pass the handler function, not to call it:不。事件处理程序的语法是传递处理程序函数,而不是调用它:

label.on('selected', functionToCall);

You might want to try label.on('selected', onLabelSelection.bind(this)) or - since this inside createLablel is apparently undefined - just label.on('selected', onLabelSelection) .您可能想尝试label.on('selected', onLabelSelection.bind(this))或 - 因为thiscreateLablel中显然是undefined - 只是label.on('selected', onLabelSelection)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OverlayManager错误。 未捕获的TypeError:无法读取未定义的属性'hasOwnProperty' - OverlayManager Error. Uncaught TypeError: Cannot read property 'hasOwnProperty' of undefined 类型错误:无法读取未定义的属性“hasOwnProperty” - TypeError: Cannot read property 'hasOwnProperty' of undefined 灰烬数据保存:TypeError:无法读取未定义的属性“ hasOwnProperty” - Ember Data Save: TypeError: Cannot read property 'hasOwnProperty' of undefined Carto 引发 TypeError:无法读取未定义的属性“hasOwnProperty” - Carto raises TypeError: Cannot read property 'hasOwnProperty' of undefined 未捕获的TypeError:无法读取未定义的属性“未定义” - Uncaught TypeError: Cannot read property 'undefined' of undefined 未捕获的TypeError:无法读取未定义的属性“ on” - Uncaught TypeError: Cannot read property 'on' of undefined 未捕获的类型错误:无法读取未定义的属性“scrollHeight” - Uncaught TypeError: Cannot read property 'scrollHeight' of undefined 未捕获的TypeError:无法读取未定义的属性“ add” - Uncaught TypeError: Cannot read property 'add' of undefined 未捕获的TypeError:无法读取未定义的属性'GetLatdlngd' - Uncaught TypeError: Cannot read property 'GetLatdlngd' of undefined 未捕获的TypeError:无法读取未定义的属性“获取” - Uncaught TypeError: Cannot read property 'fetch' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM