Wikipedia talk:User scripts/Guide: Difference between revisions

Content deleted Content added
User settings: global variables declared with var are properties of the window object; now that the let keyword is widely available, using it is preferable
User settings: Replying to Isaacl (using reply-link)
Line 62:
::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)
::I forgot that in the context of a browser, global variables declared with <code>var</code> are properties of the window object... That being said, now that the <code>let</code> keyword has been long available, it's highly preferable to use <code>let</code> to define globals, keeping the window object clear of non-standard properties. [[User:Isaacl|isaacl]] ([[User talk:Isaacl|talk]]) 23:21, 10 February 2021 (UTC)
:::I don't think <code>let</code> works at all in common.js. Replacing your common.js with <code>let test1 = true;</code> results in {{tq|VM5431:1 JavaScript parse error (scripts need to be valid ECMAScript 5): Parse error: Missing ; before statement in file 'User:Novem_Linguae/common.js' on line 1}}. I think this is what threw me off originally, causing me to give up and switch to <code>window.foo = 'bar';</code>. Now that I tested some more just now, <code>var foo = 'bar';</code> in common.js works, and a <code>try{}catch{}</code> block in the child file also works, as long as you use <code>var</code>. Anyway, I can switch the guide to <code>var</code> if you guys think it's better, or something else. Any specific suggestions? –[[User:Novem Linguae|<span style="color:limegreen">'''Novem Linguae'''</span>]] <small>([[User talk:Novem Linguae|talk]])</small> 11:39, 11 February 2021 (UTC)