简体   繁体   English

JavaScript错误“未定义不是对象”

[英]JavaScript Error “Undefined is not an object”

I am attempting to debug javascript that runs inside Adobe indesign. 我正在尝试调试在Adobe indesign中运行的javascript。 The first executable line returns an "undefined is not an object" 第一个可执行行返回“未定义不是对象”

if ( app.documents.length==0 ) { exit(); }

Could Adobe have moved the nested object "documents" to a higher level? Adobe是否可以将嵌套对象“文档”移到更高的级别?

Your code can be safe but to be effective make sure script is properly targeted. 您的代码可以安全但有效,请确保脚本已正确定向。 Ie if your debug is processing from ESTK - choose a target application from left-up corner dropdownlist or place this line in code's very top: 也就是说,如果您的调试是从ESTK处理的-从左上角的下拉列表中选择目标应用程序,或将此行放在代码的最顶部:

#target indesign 

Cashmirek is probably right. Cashmirek可能是正确的。 If you are running your script from inside ExtendScript ToolKit, you need to either type in the #target instruction or pick InDesign in the application list. 如果要从ExtendScript ToolKit内部运行脚本,则需要输入#target指令或在应用程序列表中选择InDesign。

To be safe I would suggest, you use something like this: 为了安全起见,我建议您使用如下所示的内容:

if (app === undefined || app.documents === undefined || app.documents.length === 0) { 
    exit();
}

The exit() will be performed if app or app.documents or app.documents.length is undefined or app.documents.length is 0 . 如果未定义appapp.documentsapp.documents.lengthapp.documents.length0则将执行exit() It eliminates the exception problem, because Javascript will stop evaluating the conditions, after one found 'true'. 它消除了异常问题,因为Javascript在发现“ true”后将停止评估条件。

Be sure to use === instead of == to ensure type comparison. 确保使用===而不是==来确保类型比较。

Hope that helps 希望能有所帮助

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

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