9 ms·
Most URLs are syntactically valid JavaScript code
- recursive 5y agoC# too
- varajelle 5y agoC too, C++ too
- Goety 5y agojavascript delenda est
- labster 5y agoJavascriptus delenda est. Or: Ceterum autem censeo Javascriptum esse delendam.
- awslattery 5y agoDarmok and Jalad at Tanagra.
- beebeepka 5y agoMaybe one day I will look back at that episode and conclude it was fine, even good
- G3rn0ti 5y ago„JavaScriptus delendus est.“ But I think the parent refers to the famous quote by Cato Censorius who demanded Carthago to be destroyed. He used the „accusativo cum infinitivo“ construct to express his personal desire not something that actually happened to this point. So it should read: [Ceterum censeo] Javascriptum esse delendum.
- labster 5y agoI don’t actually know Latin, just enough to know that the declension for scriptus should be applied. Thanks for fixing!
- nchase 5y agoI wanted to read this, but the styling of the website cuts diagonally across the content on my tablet in portrait mode. It makes the article unreadable...
- hoten 5y ago> The lowdown: you can copy and paste just about any website directly into your JavaScript code without modification and it won't break anything. Well, unless your code relies on an `http` or `https` label :)
- ygra 5y agoAnd it also won't work between member declarations of a class. Basically it only works where statements are legal.
- cheeze 5y agoNo different than Java or any lang that supports labels and c style comments
- Denvercoder9 5y agoTechnically it's C++-style comments, the original C only had /* */ comment blocks.
- sillysaurusx 5y agoThe URL https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... is also close to valid code as an expression. Consider something like: foo ? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label I was hoping that was where the post was going with this :) Sadly, JavaScript doesn't have the // operator (floor division) so it doesn't quite work. And double-sadly, I can't think of a language that has all the necessary ingredients; Python uses `and` and `or` for trinary syntax, not ? and :, and besides, you'd need to be able to overload the // operator as unary rather than binary, which ... well, I'm not sure we'd want a language to be able to do that. But I do! Still, if you want to confuse your coworkers, consider sneaking in something like: developer = {mozilla: 42} en = US = docs = web = Web = JavaScript = Reference = Statements = label = 420 console.log(developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label) and be sure to hand in your resignation after pushing the commit.
- valenterry 5y agoScala can do it, except for // starting a comment, so it would have to be foo ? https:/developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label which of course isn't as cool. Still pretty close though.
- eru 5y agoShould work in Haskell. You can add your own operators there.
- whateveracct 5y agoCould probably make it actually valid Haskell too, not just syntactically
- eru 5y agoYes, definitely. You would just have to define all those variables and operators. You could very probably even make it evaluate to the right URL in the end.
- 13of40 5y agoAt some point in the distant past I was working on a tool that would identify PowerShell script using the built in parsing API. It was astonishing the wide variety of files that parsed without error, including a lot of XML files.
- soheil 5y agoAwesome! It's basically a label either https: or http: that gets nothing assigned to it since the // turns everything afterwards into a comment.
- Sebb767 5y agoI was actually not aware that JavaScript supports labels. Quite funny since, usually, these articles are about quirks in JS that basically no other language has.
- zmix 5y agoOne also could look at it the other way around: an URL always matches any string, that includes Javascript code. Here is a nice example from the real world: Whenever I use the Regex engine of XML Schema (or later XPath), with the regular expression shown in RFC3986 - Uniform Resource Identifier (URI): Generic Syntax, I get the error: "Regular expression matches any string.". (The reason for this being an error is, that the XSD regex engine had only one purpose: to constrain a string, so we can check for valid input or define a datatype. That's why it behaves that way.) Here is the part with the regex from Appendix B[1] of aforementioned RFC: The following line is the regular expression for breaking-down a well-formed URI reference into its components. ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? 12 3 4 5 6 7 8 9 The numbers in the second line above are only to assist readability; they indicate the reference points for each subexpression (i.e., each paired parenthesis). We refer to the value matched for subexpression <n> as $<n>. For example, matching the above expression to http://www.ics.uci.edu/pub/ietf/uri/#Related results in the following subexpression matches: $1 = http: $2 = http $3 = //www.ics.uci.edu $4 = www.ics.uci.edu $5 = /pub/ietf/uri/ $6 = <undefined> $7 = <undefined> $8 = #Related $9 = Related where <undefined> indicates that the component is not present, as is the case for the query component in the above example. Therefore, we can determine the value of the five components as scheme = $2 authority = $4 path = $5 query = $7 fragment = $9 [1]: https://datatracker.ietf.org/doc/html/rfc3986#appendix-B https://datatracker.ietf.org/doc/html/rfc3986#appendix-B
- lifthrasiir 5y agoNot exactly. The original URI syntax didn't allow several characters including "{|`><}\^ plus whitespaces anywhere; the quoted section of RFC 3986 assumes that the string is already well-formed (thus the regex can't be used for the validation). Even the modern URL Standard has an outright failure [1]. [1] https://url.spec.whatwg.org/#example-url-parsing https://url.spec.whatwg.org/#example-url-parsing
- formerly_proven 5y ago
- soheil 5y agoSo even a data URI can be valid JS syntax data:document/document,alert('hi') If ran as JS it will alert "hi". If put into a link it will force the browser to download a document containing the alert statement.
- lucideer 5y agoThat data URI happens to use `document` which happens to be a browser global, so most data URIs won't work. e.g. running the above in Node gives: Uncaught ReferenceError: document is not defined For anyone unfamiliar with JS, the above string is parsed as: (label): (variable cast to int) / (variable cast to int), // evals to NaN (function call) // evals
- pcr910303 5y agoRemove the protocol part, and with the JS Proxy[0], you can actually do something like: await www.example.org as demonstrated in a tweet[1]. This is pretty interesting IMO :-) [0]: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy http://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer... [1]: https://twitter.com/rreverser/status/1138788910975397888 https://twitter.com/rreverser/status/1138788910975397888
- hntestacc 5y agoTesting, sorry for your inconvenience.
- danbst 5y agoNix has first-class support for URLs. Like, there are strings ("https://example.org https://example.org") and URLS (https://example.org https://example.org). And if URL is not valid, it will be parser error. However, this "feature" turned out to be misfeature and may be removed in future. The RFC: https://github.com/NixOS/rfcs/blob/master/rfcs/0045-deprecate-url-syntax.md https://github.com/NixOS/rfcs/blob/master/rfcs/0045-deprecat...
- benrbray 5y agoWhenever I come back to writing JavaScript after a long break, I always forget how to correctly return objects from lambda expressions, and end up with this: () => { foo: "bar"; baz: "qux"; } Thankfully TypeScript warns me it's a ()=>void function instead of whatever was expected in the context. I scratch my head for a while and then remember: () => ({ foo: "bar", baz: "qux" });
- Rompect 5y agoI'm more baffled that foo: "bar"; is a valid expression. I know, it's the named statement syntax normally used for loops, but I didn't know this worked for any expression.
- rudian 5y agoIn JavaScript statements don’t have to do anything. "hi"; 1; true; new Date(); () => {}; This is valid JavaScript
- madeofpalk 5y agoUnused label.ts (7028) 'https:' is defined but never used. (eslintno-unused-labels) Delete `https:` (eslintprettier/prettier)
- bumblebritches5 5y agoWonder how this fits in with Pegasus...
- MaanuAir 5y agoI can remember putting the URL of the Matrix movie website, in some plain Java code that dealt with well, matrices. What I did not know is my code was more or less secretly reviewed by a senior engineer, who was a bit surprised, but learnt something he told me. This is how the secret review became public. My red pill, indeed! edit: typo
- honie 5y ago> The lowdown: you can copy and paste just about any website directly into your JavaScript code without modification and it won't break anything. The following doesn't seem to be valid though. :( Am I missing something? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label let a = 1;
- maxbond 5y ago``` >> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... 10 <- 10 ``` Eg, this is equivalent to: ``` >> https: 10 ```
- lhorie 5y agoSort of the same reason why `if (true) let a = 1` is a syntax error (and the error message says why: "Lexical declaration cannot appear in a single-statement context") The rationale here is that the variable goes out of scope right away due to the implicit block, so they made it a syntax error so that you don't waste time pulling your hair out debugging issues downstream from this line without realizing this scope-related obscurity. (This also has potential security implications e.g. if you think you're shadowing a variable from a parent scope but you're not)
- draegtun 5y agoAnd in Rebol & Red URL's are syntactically valid datatypes (literals): http://www.rebol.com/r3/docs/datatypes/url.html http://www.rebol.com/r3/docs/datatypes/url.html For eg. in Red console: >> read https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label == {<!DOCTYPE html><html lang="en-US" prefix="og: https://ogp.me/ns#"><head><me...
- specialist 5y agoI want this. Esp in DSLs. Bonus points for first class support for fragments and so forth. (aka Intrinsics?)
- draegtun 5y agoYou can have fragments (and so forth) with Rebol/Red URLs. For eg. >> type? https://www.example.com/foo.html#bar == url! >> type? https://www.example.com/foo.cgi?bar=1&baz=2 == url!
- philo23 5y agoThis is a neat trick, but I'm more interested in the little remark about how Svelte uses the $: label https://svelte.dev/docs#3_$_marks_a_statement_as_reactive https://svelte.dev/docs#3_$_marks_a_statement_as_reactive Is Svelte doing something clever with these labels at runtime, or is it just using the label to do some kind of transformation when the javascript is compiled, anyone know?
- nicoburns 5y agoI believe it's parsing the JavaScript and doing a transformation. That's kind of svelte's USP: better ergonomics because it goes beyonds what's possible in standard JS.
- paco3346 5y agoTransformation is the name of the game with Svelte. None of the Svelte syntax is run at runtime because it's all compiled down to vanilla JS (and therefore ships with 0 runtime libraries).
- deleted 5y ago[deleted]
- mpweiher 5y agoNeat, but as TFA says, not useful. Objective-S makes URIs useful: http://objective.st/URIs/ http://objective.st/URIs/
- lhorie 5y agoSemi-related, there's a a useful trick for using Javascript labels as URLs. Way back in the day, I used to write labels like `<a href="javascript:open_save_dialog:;">` instead of `<a href="javascript:void(0)">`. The idea is that both yield undefined to prevent the default URL following semantics of the link, to instead do whatever JS logic is attached to the onclick event, but when the user hovers over the link, the label makes the corner tooltip of the browser show descriptive information to the user about what clicking will do, whereas `void(0)` tells you nothing.
- empyrical 5y agoYou really should not use this ever in any situation, but for historical reasons[1], if they are the only thing on the line you can use HTML comments in JavaScript like so: function foo() { <!--- returns true ----> return true; } This works at least in Chrome and V8, not sure about Firefox. [1] https://stackoverflow.com/a/1508005/1888964 https://stackoverflow.com/a/1508005/1888964
- lhorie 5y agoSemi-related: https://twitter.com/LeoHorie/status/930515291183898624 https://twitter.com/LeoHorie/status/930515291183898624
- nsonha 5y agoone of my earliest programming qirk encounters was scratching my head over why `() => { key: value }` returns nothing