Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

[Help] So confused...

if (PowerCard.leftsub === undefined) var PowerCard.leftsub = (PowerCard.usage !== undefined) ? PowerCard.usage : ""; This is just weird... it throws an unexpected identifier error unless I remove var from the line... but I have not defined PowerCard.leftsub anywhere else and it should need it. Why is it doing this?
1393715835
Alex L.
Pro
Sheet Author
Try if (PowerCard.leftsub === undefined) { var PowerCard.leftsub = (PowerCard.usage !== undefined) ? PowerCard.usage : ""; }
I did and it still threw the error. It works even without using var... so I'm not too concerned. I just want to know what this quirk of javascript is...
1393771380

Edited 1393771636
Properties can be created on the fly. Same for other variables, if you don't declare a variable in JavaScript, but implicitly use it, it is created at a global scope. I don't believe you can declare properties of a class with that Syntax which is why it is throwing an error.
George B. said: Properties can be created on the fly. Same for other variables, if you don't declare a variable in JavaScript, but implicitly use it, it is created at a global scope. I don't believe you can declare properties of a class with that Syntax which is why it is throwing an error. No, you can... I do it all the time. As soon as I remove var from the line, it works.
1393787211

Edited 1393787465
Sorry, my point was that having the word var there is incorrect syntax for adding a new property to an object in Javascript. the 'var' would only be acceptable when declaring a variables within the objects definition. And to clarify, using the 'var' within the objects definition would create a variable with the scope of the object, but not actually a property of the object. So, that may or may not actually be what you want.
1393797067
Lithl
Pro
Sheet Author
API Scripter
PowerCard.leftsub isn't a variable, it's a property. PowerCard is a variable. PowerCard was declared with var; leftsub is not.
Aha... that error makes sense now.