JS原型:未捕获的类型错误未定义不是一个函数

JS prototype: uncaught typeerror undefined is not a function

本文关键字:函数 一个 未定义 错误 原型 类型 JS      更新时间:2023-09-26

我在尝试在 AngularJS 中创建工厂时遇到了麻烦。我只是将控制器中的代码移动到工厂,并进行一些更改即可正常工作。

这是我遇到的错误:

"El objeto no acepta la propiedad o el método 'addPoint'" (in IE)("对象不支持属性或方法'addPoint'")

"未捕获的类型错误:未定义不是一个函数"(在 Chrome 中)

这是我的代码:

function Shape (shape) {
  this.ToolName   = shape.tool;
  this.LineColor  = shape.lineColor;
  this.LineWidth  = shape.lineWidth;
  this.LineCap    = shape.lineCap;
  this.FillStyle  = shape.fillStyle;
  this.isFilled   = shape.filled;
  this.isStroked  = shape.stroked;
  this.Points     = [];
}
Shape.prototype.addPoint = function(point){
  if(this.ToolName!=='pencil' && this.Points.length>1){
    this.Points.pop();
  }
  this.Points.push(point);
};

这曾经在控制器内部工作,但现在在工厂内部则不工作。

谢谢。

编辑:

很抱歉我没有分享更多的代码。这里有源代码:https://github.com/michaeljota/Arheados/blob/master/app/scripts/controllers/main.js

这是控制器的代码。我想做的是将$scope无法处理的所有内容转换为工厂。有人建议使用服务,但我不太了解服务的工作原理。

再次感谢!

感谢Evan Knowles,阅读我的代码,我发现了错误。

我没有给构造函数形状,只是tmpShape = shape;(女巫是$scope模型)。所以我只是把它修复到这个tmpShape = new Shape(shape);它又可以工作了。

对于这样一个菜鸟问题,我真的很抱歉。

谢谢大家!