Copy link to clipboard
Copied
i try to read from my file (if it exists) string for getting print style name. I did this in function.
var mSty = ''; // initiated variable
var mIs; // var for exist-yes/no-indicator
function findFile(mIs, mSty) {
fname = app.activeDocument.filePath + '/' + 'prn_style.txt';
if(!File(fname).exists) {
mIs = false
}
else
{
mIs = true
var f = File(fname); ////////////////////////
f.open("r");
while(!f.eof) {
mSty = f.readln();
}
f.close();
} // end if exists
return mIs;
return mSty; ///// here "mSty" stil DOES exist...
}
///////and here "mSty" already DOESN't exit. Why?
if (mSty == null) alert ('no file') else alert(mSty);
So i can't get a string from file, cause variable dissapears.
In breif: i need to get two variables from one function.
hi,
you don't call the function (at least in the example), return means return ;-), ...
try
var test = someFunction();
function someFunction() {
var01 = 'funny';
var02 = true
return [var01, var02];
}
alert(test[0] + ' | ' + test[1]);
Copy link to clipboard
Copied
hi,
you don't call the function (at least in the example), return means return ;-), ...
try
var test = someFunction();
function someFunction() {
var01 = 'funny';
var02 = true
return [var01, var02];
}
alert(test[0] + ' | ' + test[1]);
Copy link to clipboard
Copied
Let me guess: you have a Pascal background. javascript is more close to C++.
so, you don't need the variables in the function definition. That just becames:
function findFile(){
you cannot have two return like that in your function. a function can retun only one object.
so instead of:
return mIs;
return mSty; ///// here "mSty" stil DOES exist...
use:
return [mls,mSty];
in order to use the funtion, you have to call it somwhere in your code.
function findFile() {
var mSty = ''; // initiated variable, declared local
var mIs; // var for exist-yes/no-indicator
fname = app.activeDocument.filePath + '/' + 'prn_style.txt';
if(!File(fname).exists) {
mIs = false
}
else
{
mIs = true
var f = File(fname); ////////////////////////
f.open("r");
while(!f.eof) {
mSty = f.readln();
}
f.close();
} // end if exists
return [mIs, mSty]; ///// here "mSty" stil DOES exist...
}
var myTest=findFile(); //calling the function and storing it's resut in a variable;
if (myTest[0] == null) alert ('no file') else alert(myTest[1]);
//the findFile function retuns a object (array).
//The first member of that array is mIs, the second mSty
Copy link to clipboard
Copied
Thanks, here i learned, that function returns once, but full of array!
Copy link to clipboard
Copied
"this" is a very useful alternative to Han's code
var test = someFunction();
alert(test.fun + ' | ' + test.t);
function someFunction()
{
this.fun = 'funny';
this.t = true
return this
}
There are pros and cons of both methods;
One consideration is that a result stored in an array is can be looped
On the other hand it can be a lot easier for mortals to remember something like "fun" than result[0].
this should help
Trevor
Copy link to clipboard
Copied
true
😉
Copy link to clipboard
Copied
yep, and from here we go OOP route, which is the way it should be, but seeing as OP has problems with basic procedural programming, i thought to keep it simple
Copy link to clipboard
Copied
Hi ~Trevor~
A potential issue in your code—although in some case this is an interesting feature!—is that your test variable is then bound to someFunction's context.
A slightly modified code will show my point:
function someFunction()
{
this.value = Math.random();
return this;
}
var test1 = someFunction();
alert( test1.value ); // some random number
var test2 = someFunction(); // new call
alert( test1.value ); // changed!!
// In fact, we have:
alert( test1===test2 ); // true
In other words, someFunction will always update a single instance and return a reference to that single instance. The reason for this is that you do not call the function as a constructor. So, if you plan to re-use someFunction to address several objects, you should use new someFunction() instead:
function someFunction()
{
this.value = Math.random();
return this;
}
var test1 = new someFunction();
alert( test1.value ); // some random number
var test2 = new someFunction(); // new instance
alert( test1.value ); // unchanged
alert( test1===test2 ); // false
This illustrates the crucial difference between myFunc() and new myFunc().
Anyway, if someFunction.prototype is not customized (i.e. you don't need dedicated methods for those objects), then there is no reason to use this. You could return as well an object instance that you build on the fly:
function someFunction()
{
return {
fun: 'funny',
t: true
};
}
var test = someFunction();
alert(test.fun + ' | ' + test.t); // => funny | true
which is the exact counterpart of the Array's approach:
function someFunction()
{
return [
'funny',
true
];
}
var test = someFunction();
alert(test[0] + ' | ' + test[1]); // => funny | true
@+
Marc
Copy link to clipboard
Copied
Marc, any chance you'll start an advance javascript class, or maby a book? i'll sign up in a heartbeat.
Copy link to clipboard
Copied
Thanks, Vamitul
But, there are already so many great resources on this subject…
@+
Marc
Copy link to clipboard
Copied
Do share, please. I'm still struggling with oop in javascript (and making sense of most of your code).
And (sorry for hijacking the thread), but if some of the AS gurus around can share beginners level documentation to get me stared with extensionBuilder, i would be very grateful.
Copy link to clipboard
Copied
> Do share, please.
Give a look at #119 here:
@+
Marc
Copy link to clipboard
Copied
Thanks Marc,
Very clearly illustrated.
Trevor