"数组不是函数"错误

Bar*_*ney 1 javascript arrays photoshop

我正在尝试创建一个在Photoshop中执行以下操作的JS脚本:

var textarray = array("Hello World", "Good morrow", "top of the morning");
Run Code Online (Sandbox Code Playgroud)

对于数组中的每个单词

  1. 打开新文档
  2. 将单词写入图层
  3. 运行photoshop操作
  4. 保存并关闭

到目前为止这是我的代码..

var textarray = [ "Hello World", "Good morrow", "top of the morning" ];


for (x=0; x < textarray.length(); x++) {


#target photoshop
app.bringToFront();

var strtRulerUnits = app.preferences.rulerUnits;
var strtTypeUnits = app.preferences.typeUnits;
app.preferences.rulerUnits = Units.INCHES;
app.preferences.typeUnits = TypeUnits.POINTS;

var docRef = app.documents.add(7, 5, 72);

// suppress all dialogs
app.displayDialogs = DialogModes.NO;

var textColor = new SolidColor;
textColor.rgb.red = 255;
textColor.rgb.green = 0;
textColor.rgb.blue = 0;

var newTextLayer = docRef.artLayers.add();
newTextLayer.kind = LayerKind.TEXT;
newTextLayer.textItem.contents = textarray[x];
newTextLayer.textItem.position = Array(0.75, 0.75);
newTextLayer.textItem.size = 36;
newTextLayer.textItem.color = textColor;

app.preferences.rulerUnits = strtRulerUnits;
app.preferences.typeUnits = strtTypeUnits;
docRef = null;
textColor = null;
newTextLayer = null;

// DO ACTION HERE 
//CLOSE AND SAVE
}
Run Code Online (Sandbox Code Playgroud)

这是由于某种原因无法正常工作的数组部分.错误24:textarray.length不是函数

JJJ*_*JJJ 6

要回答原始问题,array( ... )不是如何在JavaScript中创建数组.

var textarray = [ "Hello World", "Good morrow", "top of the morning" ];
Run Code Online (Sandbox Code Playgroud)

至于下一个问题(实际上应该是一个单独的问题),length不是函数而是属性.

for (x=0; x < textarray.length; x++) { 
    ...
Run Code Online (Sandbox Code Playgroud)

  • 或者`var textarray = new Array("Hello World","Good morrow","top of the morning");`,这就是你想要做的事情.无论如何,请使用Juhana的语法 - 更容易做对. (2认同)