Wikipedia talk:User scripts/Guide: Difference between revisions
Content deleted Content added
→User settings: re |
→User settings: can check for existence of variable; typically would store specific values as properties of a uniquely named global |
||
Line 59:
:{{u|Isaacl}}, thanks for the feedback. I added that because I got stuck when trying to just declare the variable, so I looked at how other user scripts did it. [[WP:REPLYLINK]] and some others use window variables. However, if there is a better way of doing it that accomplishes the same thing, I am certainly open to suggestions. –[[User:Novem Linguae|<span style="color:limegreen">'''Novem Linguae'''</span>]] <small>([[User talk:Novem Linguae|talk]])</small> 08:04, 10 February 2021 (UTC)
::In general, if you're going to declare a global, it should be named something that would never be used by anything else. Which is usually done by prefixing it with the name of the script. [[User:Nardog|Nardog]] ([[User talk:Nardog|talk]]) 08:16, 10 February 2021 (UTC)
::If the concern is [//en.wikipedia.org/w/index.php?title=Wikipedia:User_scripts/Guide&diff=1005959567&oldid=1005951169 undeclared variable errors], then you can catch the error with a try-catch block, or check for the existence of a variable using <code><nowiki>typeof x === 'undefined'</nowiki></code>. This check won't distinguish between an undeclared or undefined variable, but for the purpose of user settings, it doesn't matter.
::In line with Nardog's comment, typically I would suggest a uniquely named global to act as a encapsulating namespace, with specific values stored as properties in the global, so their existence can be verified with <code><nowiki>hasOwnProperty()</nowiki></code> or the <code>in</code> operator. [[User:Isaacl|isaacl]] ([[User talk:Isaacl|talk]]) 15:51, 10 February 2021 (UTC)
|