繁体   English   中英

indesign中的isPrototypeOf

[英]isPrototypeOf in indesign

嗨,您是Indesign脚本设计的新手,想弄清楚对象是否是类的子类型。 示例:我想遍历所有页面项并接受所有非图形的内容:

layer = app.activeDocument.layers[layerIndex];

for (i = 0; i < layer.allPageItems.length; i++) {
  alert(layer.allPageItems[i].reflect.name)
  if(layer.allPageItems[i].isPrototypeOf (Graphic) ) {
    alert("Graphic");
  } else {
    ....
  }
}

如果nver匹配。 是否有使用isPrototypeOf 我该怎么做才能测试对象是某种类型还是其子类?

编辑 :澄清一下,我正在尝试测试是否有从Graphic 继承的任何实例。

但据我现在所知,这似乎是不可能的。

您可能需要instanceof运算符。

if (layer.allPageItems[i] instanceof Graphic) {
    alert("Graphic");
} else {
    ....
}

您也可以使用isPrototypeOf但必须颠倒顺序并获取原型本身,而不是构造函数。 所以它看起来像这样:

if (Graphic.prototype.isPrototypeOf(layer.allPageItems[i])) {
    alert("Graphic");
} else {
    ....
}

您可以通过调用getElements()方法来访问pageItem的本质。 它返回原始材料的数组。 给定页面上的矩形(没有其他内容):

app.activeDocument.allPageItems[0].getElements()[0] instanceof Rectangle //true;

你确定它不应该是

Graphic.isPrototypeOf(layer.allPageItems[i])

或类似的东西

Graphic.prototype.isPrototypeOf(layer.allPageItems[i])

您当前的版本听起来像是倒退了。

显然这是不可能的,我也在adobe论坛上询问了以下结果: http : //forums.adobe.com/message/4461211#4461211

因此,简短的答案是,我无法检查我持有的对象是否为someClass的someClass或其子级。 反射和isPrototypeOf都不isPrototypeOf帮助。

我可能会尝试在try catch块中进行投射,但认为这很丑陋。 因此,我将使用adobe论坛上建议的解决方案,测试所有可能的继承人(从基层继承的子代/类)和基类。 这是丑陋且冗长的,但我还没有找到更好的解决方案。

编辑 :这是一个adobes示例的摘录,它允许使用switch语法避免无穷的if构造:

switch (app.selection[myCounter].constructor.name){
    case "Rectangle":
    case "Oval":
    case "Polygon":
    case "GraphicLine":
    case "TextFrame":
        myObjectList.push(app.selection[myCounter]);
        break;
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM