Variable number or array
Copy link to clipboard
Copied
I have a variable that could be a number or an array of 4 numbers to use. What I have is:
if (Number(mTBLR)) {
top = mTBLR;
bottom = mTBLR;
left = mTBLR;
right = mTBLR;
}
if (mTBLR.constructor == Array && mTBLR.length == 4) {
top = mTBLR[0];
bottom = mTBLR[1];
left = mTBLR[2];
right = mTBLR[3];
}
Is checking the constructor a good way to go I could not find much in my docs on this?
Also while on this is there a simple way to test the number is integer
Explore related tutorials & articles
Copy link to clipboard
Copied
For core javascript objects, the Adobe scripting guides are not much help. I like http://www.w3schools.com/jsref/ and if that doesn't have what I'm looking for then Google.
To test for integers you could do lots of things. I would start with something like this
// make sure that it a number and an integer
if (mTBLR.constructor == Number && parseInt(mTBLR) ==mTBLR )
Copy link to clipboard
Copied
I would do it more like this:
if (mTBLR instanceof Array) {
if (mTBLR.length == 4) {
top = mTBLR[0];
bottom = mTBLR[1];
left = mTBLR[2];
right = mTBLR[3];
} else {
// it's an array but the length isn't 4
}
} else if (Number(mTBLR)) {
top = mTBLR;
bottom = mTBLR;
left = mTBLR;
right = mTBLR;
}
The only time you really need to do 'x.constructor' is when checking for Strings. String literals and String objects both have the constructor 'String'. However, literals are not proper objects and are, therefore, not an instance of the String class. But that's another rant...
Copy link to clipboard
Copied
Google found me both 'constructor' &' instanceof' but google can also find stuff i've wrote and not always the greatest of examples.
So I thought I'd ask the pro's before getting myself into trouble
Thank you both
Midnight and Im still sat here trying to learn this stuff must be going mad
Copy link to clipboard
Copied
xbytor2 wrote:
The only time you really need to do 'x.constructor' is when checking for Strings. String literals and String objects both have the constructor 'String'. However, literals are not proper objects and are, therefore, not an instance of the String class. But that's another rant..
You are right if he is sure that mTBLR can only be a Array or a Number, but I added it as a double check because '200px' or 2.35 would both test false with if(parseInt(mTBLR) == mTBLR) and it wouldn't be clear why. The first because parseInt returned NaN and the second because it's not an integer. I guess that if it doesn't matter why it failed then just if(parseInt(mTBLR) == mTBLR) would work.
But I am open to a better way to test for integer. Doesn't if(Number(2.75)) test true and if(Number(0)) test false?
Copy link to clipboard
Copied
Using the Number() function/constructor to test is incorrect unless your real condition is 'is this a valid non-zero number'. The correct test would be
if (!isNaN(mTBLR)) {
//...
}
I also tend not to use parseInt. Either I know the entire string is supposed to be a number or I'm going to RegExp it to get the number-part out, then use Number for the conversion.
Also, parseInt("077") != Number("077").
Copy link to clipboard
Copied
Sorry to be dim X, but doesn't !isNaN(7.98) test true. Mark wanted a test for integer values.
Copy link to clipboard
Copied
I'm the dim one today, Michael. I'm only half reading before I post stuff.
My test for an integer would be !!x.toString().match(/^\d+$/); but the check in your post earlier would work as well.
Copy link to clipboard
Copied
Now I'm being nit picking, what about negative integers?
var mTBLR = -1;
alert((mTBLR.constructor == Number && parseInt(mTBLR) ==mTBLR ));
alert(!!x.toString().match(/^\d+$/));
Copy link to clipboard
Copied
/[-+]?\d+/
🙂
Copy link to clipboard
Copied
Well thank you guys I was feeling a little thick that I could not see an obvious way to do this in JavaScript and your tests appear better thought thru than mine (thats why it's better to ask) I did try '!isNaN' with what Im used to this is easy. Its just sometimes the little things that can stop you in your tracks and have you scratching your head for the correct method.
set x to 1234.567
get class of x
-- returns real
set y to 1234567
get class of y
-- returns integer
Copy link to clipboard
Copied
Muppet Mark wrote:
Its just sometimes the little things that can stop you in your tracks and have you scratching your head for the correct method
I agree and because it's something new you can be in for more head scratching if you are not careful. I know that I have done my share.
So the back and forth here is not about who is correct but what is correct test for a integer.
var mTBLR = "+20-10";
alert(!!mTBLR.toString().match(/[-+]?\d+/));
Even if you change the re to /^[-+]?\d+$/ gives a test for integers or correctly formatted string.
If there is a problem using parseInt in this case it can be replaced by one of several math methods like Math.round. Or the entire second expression can be replaced with !( mTBLR % 1) but you still need to make sure that mTBLR is not a string and those test can't do that because javascript is happy too convert a string to a number.
So I don't see a way to avoid x.constructor. I agree that both constructor and parseInt can be misused, but that doesn't mean they shouldn't never be used.
Copy link to clipboard
Copied
There already has been and Im sure there will be plenty more head/desk banging going on. That's all part & parcel of the learning curve some are steeper than others the reason I posted was during one of my basic tests but I can't remember which now. Off the top of my head I think it was this…
#target photoshop
var x = [5]
if (isNaN(x)) {
alert('True');
}
else {
alert('False');
}
// returned false where I did NOT expect it too? Looks like [] were lost in translation but I did not know why…
set x to "123.456"
get class of x
-- returns "string" no attempt to convert unless I ask it to which some times is nice when you are at the bottom end of the curve
Copy link to clipboard
Copied
isNaN is a test for the special value NaN because normal == tests fail
var n = Number('r');// NaN
n == NaN;// false
isNaN(n);// true
isNaN(5);// false
So !isNaN(x) doesn't mean x is a number. It means it's not NaN.

