Wikipedia talk:User scripts/Guide: Difference between revisions

Content deleted Content added
User settings: Replying to Isaacl (using reply-link)
User settings: update my comment
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 [https://en.wikipedia.org/wiki/User:Novem_Linguae/Scripts/VariableTest.js also works], as long as you use <code>var</code>. Anyway,While Ithat can<code>try{}catch{}</code> switchblock theworks, I guidestill tolike <code>varwindow.foo = 'bar';</code>, ifbecause youthe guys<code>try{}catch{}</code> thinkblock it'sis better, or something else[https://en.wikipedia.org/wiki/User:Novem_Linguae/Scripts/VariableTest.js Anyvery specific suggestions?verbose]. –[[User:Novem Linguae|<span style="color:limegreen">'''Novem Linguae'''</span>]] <small>([[User talk:Novem Linguae|talk]])</small> 11:39, 11 February 2021 (UTC)