Copy link to clipboard
Copied
A lot of code snippets I find seem to have "new" in it. I'm trying to figure out exactly what it does. I've been googling and can't seem to really figure it out. I've tested a lot of things with and without "new" and it doesn't seem to make a difference. For example, these to lines of code both work with or without "new". So is "new" really needed and if so why?
app.activeDocument.activeLayer.translate(new UnitValue(1000,'px'),new UnitValue(1000,'px'));
app.activeDocument.activeLayer.translate(UnitValue(1000,'px'),UnitValue(1000,'px'));
Copy link to clipboard
Copied
According to the official JAVASCRIPT TOOLS GUIDE documentation:
https://wwwimages2.adobe.com/content/dam/Adobe/en/devnet/scripting/pdfs/javascript_tools_guide.pdf
page 230:
The UnitValue constructor creates a new UnitValue object. The keyword new is optional:
myVal = new UnitValue (value, unit);
myVal = new UnitValue ("value unit");
myVal = new UnitValue (value, "unit");
[...]
For example, all the following formats are equivalent:
myVal = new UnitValue (12, "cm");
myVal = new UnitValue ("12 cm");
myVal = UnitValue ("12 centimeters");
HTH...
Copy link to clipboard
Copied
Copy link to clipboard
Copied
You should use "new" when you want create instance of class.