【问题标题】:Why does my object not have a constructor?为什么我的对象没有构造函数?
【发布时间】:2011-05-18 13:03:05
【问题描述】:

我正在编写一些用于编写 Adob​​e Illustrator 脚本的帮助类。

我的问题始于 Eyelet 对象。当我实例化它时,它在第一个 new Group() 处失败,因为显然 Group 没有构造函数。

这是我的代码的精简版:

/****************** Collection Class **********************/

function Collection() {
    this.parent = app.activeDocument;
    this.typename = "Collection";
}

    Collection.prototype.setName = function(name) {
        this.instance.name = name;
    };

/****************** Group (extends collection) *****************/

function Group(name, parent) {
    this.parent = parent || this.parent;
    this.instance = this.parent.groupItems.add();
    if(name) {
        this.setName(name);
    } else {
        this.setName("Group");
    }
}

Group.prototype = new Collection();

/****************** Shape Class **********************/

function Shape() {
    this.parent = app.activeDocument;
    this.typename = "Shape";
}

Shape.prototype.setName = function(name) {
        this.instance.name = name;
    };

Shape.prototype.stroke = function(width, color) {
        this.instance.stroked = true;
        this.instance.strokeWeight = width;
        this.instance.strokeColor = color;
    };

/****************** Line (extends Shape) **********************/

function Line(parent, start, end) {

    this.instance = parent.pathItems.add();
    // [ [startX, startY], [endX, endY] ]
    this.instance.setEntirePath([start,end]);

}

Line.prototype = new Shape();

/****************** Eyelet (extends Shape) **********************/

function Eyelet(parent, position) {
    this.instance = new Group("Eyelet", parent);
    var whiteCross = new Group("White", this.instance);
    var blackCross = new Group("Black", this.instance);

    var build = function(group, color, width) {
        var vertical = new Line( group , [0 , 0] , [0 , 50] );
        vertical.setName("vertical");
        vertical.stroke(width, color);
        var horizontal = new Line( group , [0 , 50] , [50 , 0] );
        horizontal.setName("horizontal");
        horizontal.stroke(width, color);
    };

    build(whiteCross.instance, white, (3 * scale) );
    build(blackCross.instance, black, (1 * scale) );

    this.instance.position = position;

}

Eyelet.prototype = new Shape();

当我写作时

var eyelet = new Eyelet(layer2, [10,10]);

我明白了

组没有构造函数

我已经通过 jslint 运行了这段代码,但我不明白为什么这不起作用。任何帮助将不胜感激。

【问题讨论】:

  • 您的代码的jsFiddle:jsfiddle.net/aditya/RqByH — 我在这里没有得到那个错误。它会引发其他一些错误,但那是因为您的代码不完整。
  • @aditya 是的,你是对的。如果我在 Group 构造函数中粘贴 alert() ,它会发出很好的警报,但它不会在 Illustrator 中。嗯……
  • 在原始代码中,您确定Group在与Eyelet相同的范围内可见吗?尝试重命名 Group() 函数,看看是否遇到相同的错误。然后尝试移动或复制 Group() 使其位于 Eyelet 的正上方,看看会发生什么。
  • @Dan 我重命名了Group,它运行良好。 Group 必须由 Illustrator 的脚本引擎使用。我的Group 班级曾经被称为group,所以我以前从未注意到这一点。谢谢。

标签: javascript prototype constructor adobe-illustrator


【解决方案1】:

事实证明,这与 Adob​​e Illustrator 中的 相关。我按照 Dan Breslau 的建议将 Group 对象重命名为 MyGroup,它按我的预期工作。似乎 Illustrator 有一个导致问题的全局 Group 对象。感谢您的帮助。

【讨论】:

    猜你喜欢
    • 2010-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 2018-01-29
    相关资源
    最近更新 更多