<?xml version='1.0' encoding='utf-8' ?>

<rss version='2.0' xmlns:lj='http://www.livejournal.org/rss/lj/1.0/' xmlns:atom10='http://www.w3.org/2005/Atom'>
<channel>
  <title>Rich and Strange Aeons</title>
  <link>https://mindstalk.dreamwidth.org/</link>
  <description>Rich and Strange Aeons - Dreamwidth Studios</description>
  <lastBuildDate>Thu, 22 Oct 2020 21:34:12 GMT</lastBuildDate>
  <generator>LiveJournal / Dreamwidth Studios</generator>
  <lj:journal>mindstalk</lj:journal>
  <lj:journaltype>personal</lj:journaltype>
  <image>
    <url>https://v2.dreamwidth.org/241388/374172</url>
    <title>Rich and Strange Aeons</title>
    <link>https://mindstalk.dreamwidth.org/</link>
    <width>100</width>
    <height>75</height>
  </image>

<item>
  <guid isPermaLink='true'>https://mindstalk.dreamwidth.org/570790.html</guid>
  <pubDate>Thu, 22 Oct 2020 21:34:12 GMT</pubDate>
  <title>more Javascript</title>
  <link>https://mindstalk.dreamwidth.org/570790.html</link>
  <description>New Job has me learning JS and Node.js.  I have learned that some of my earlier complaints (see tag) have been improved by later developments (or ones extant at the time that I hadn&apos;t found.)  Like &apos;var&apos; variables have function scope (useful concept; I think it applies to Python), but you can get block scope (One True Way) by using &apos;let&apos; instead.  I don&apos;t think I knew that simply assigning to an undeclared variable creates a global (dear gods) but JS borrowed &apos;use strict&apos;; from Perl, which shuts that off.&lt;br /&gt;&lt;br /&gt;JS strikes me as a shitty half-assed core language which is trying to grow its way into respectability.  Unfortunately it has 25 years of shitty web code to have to be backwardly compatible with.&lt;br /&gt;&lt;br /&gt;No wonder there are multiple languages to use instead, that compile down into JS.&lt;br /&gt;&lt;br /&gt;Today I accidentally discovered another WTF.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;
&amp;gt; l=[1,2,3]
[ 1, 2, 3 ]
&amp;gt; for (v in l) console.log(typeof(v));
string
string
string
undefined
&amp;gt; for (v in l) console.log(v);
0
1
2
&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;So in a way this makes sense.  &quot;for in&quot; iterates over the properties/keys of an object, which for an Object (dictionary-ish) are names.  The properties of an Array are numbers, but in a &apos;for in&apos; context, they could be strings.&lt;br /&gt;&lt;br /&gt;OTOH, stripped of rationalization: the indices of an array are numeric, and yet they&apos;re strings in this iteration case.  ffffuuuuu.&lt;br /&gt;&lt;br /&gt;Of course, JS coerces between numbers and strings at least as fluidly as Perl does.&lt;br /&gt;&lt;br /&gt;I found this behavior by accident, I was printing &apos;v[0]&apos; thinking I was using &apos;for of&quot; (which iterates over the values of an Array) when I wasn&apos;t, and I got numbers rather than letters or &apos;undefined&apos;, which is what you get if you try&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;
&amp;gt; n=4
4
&amp;gt; n[0]
undefined
&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src=&quot;https://www.dreamwidth.org/tools/commentcount?user=mindstalk&amp;ditemid=570790&quot; width=&quot;30&quot; height=&quot;12&quot; alt=&quot;comment count unavailable&quot; style=&quot;vertical-align: middle;&quot;/&gt; comments</description>
  <comments>https://mindstalk.dreamwidth.org/570790.html</comments>
  <category>programming</category>
  <category>javascript</category>
  <lj:security>public</lj:security>
  <lj:reply-count>0</lj:reply-count>
</item>
<item>
  <guid isPermaLink='true'>https://mindstalk.dreamwidth.org/564767.html</guid>
  <pubDate>Wed, 15 Jul 2020 02:39:41 GMT</pubDate>
  <title>JS surprise</title>
  <link>https://mindstalk.dreamwidth.org/564767.html</link>
  <description>I was looking at my &apos;programming&apos; tag posts and found a bunch of 3 years ago, complaining about odd features of JavaScript.  I kind of remember them now, but if you&apos;d asked me yesterday if I&apos;d ever taught myself JavaScript I would have said &apos;no&apos;, not remembering doing so.&lt;br /&gt;&lt;br /&gt;I was prompted by an interviewer having looked at this blog.  I wonder what he thought about my saying I hadn&apos;t taught myself JS...&lt;br /&gt;&lt;br /&gt;&lt;img src=&quot;https://www.dreamwidth.org/tools/commentcount?user=mindstalk&amp;ditemid=564767&quot; width=&quot;30&quot; height=&quot;12&quot; alt=&quot;comment count unavailable&quot; style=&quot;vertical-align: middle;&quot;/&gt; comments</description>
  <comments>https://mindstalk.dreamwidth.org/564767.html</comments>
  <category>javascript</category>
  <category>programming</category>
  <lj:security>public</lj:security>
  <lj:reply-count>0</lj:reply-count>
</item>
<item>
  <guid isPermaLink='true'>https://mindstalk.dreamwidth.org/466385.html</guid>
  <pubDate>Sun, 05 Feb 2017 17:35:25 GMT</pubDate>
  <title>JavaScript quirks</title>
  <link>https://mindstalk.dreamwidth.org/466385.html</link>
  <description>I&apos;ve started studying JavaScript a bit, as you might guess from recent posts.  It&apos;s struck me as a mix of Perl, Python, and crack.  It&apos;s got some neat things, especially in comparison to one language or the other.  And it&apos;s got lots of... wtf things.&lt;br /&gt;&lt;br /&gt;+: exponentiation operator; nested arrays and objects (dictionaries) (without Perl&apos;s references); first class functions and lambdas and closures, including nested functions (unlike Perl); Perl-like push/pop/shift/unshift array operators (but what&apos;s the performance?); consistent &apos;valueOf&apos; and &apos;toString&apos; methods; JSON; multiple kinds of for loops; Perl style labeled break and continue; some convenient conversions (but see below); nice Date methods.&lt;br /&gt;&lt;br /&gt;-: oh boy.&lt;br /&gt;&lt;br /&gt;* JSON stringifies nested structures nicely, but simple output doesn&apos;t: [1, [2,3]] outputs as [1, 2, 3].&lt;br /&gt;* (object1 == object2) is always false, no matter the underlying values. This holds for arrays and Date objects too.  Nothing like Python&apos;s structural equality, or even that of STL containers.&lt;br /&gt;** But you can do *inequality* comparison: ([1,2,3] &amp;lt; [2,2,3]) == true.&lt;br /&gt;* strings take negative indices a la Python, but arrays don&apos;t.  [2020 edit: what was I thinking?  string.slice can take negative indices, but character access via [] doesn&apos;t.  And array.slice also takes negative.]&lt;br /&gt;* there&apos;s a typeof operator, but it just says &apos;object&apos; for arrays.&lt;br /&gt;* &quot;5&quot;+2 == &quot;52&quot; (convert 2 to &quot;2&quot;, concatenate), but &quot;5&quot;-2 == 3 (convert &quot;5&quot; to 5, subtract.) And no Python string multiplier like &quot;a&quot;*2 == &quot;aa&quot;.&lt;br /&gt;** As Avi noted, it gets even weirder given that the values could be hidden in a variable.  a+2==&quot;52&quot;, a-2==3&lt;br /&gt;* [1,2]+[3,4] doesn&apos;t concatenate arrays, doesn&apos;t add by element, doesn&apos;t give a type error, but gives... &quot;1,23,4&quot; (turn arrays into strings, concatenate without delimiter.)&lt;br /&gt;&lt;br /&gt;My friend Mark linked me to &lt;a href=&quot;https://www.destroyallsoftware.com/talks/wat&quot;&gt;https://www.destroyallsoftware.com/talks/wat&lt;/a&gt; which gives some more:&lt;br /&gt;* []+{} == [object Object]&lt;br /&gt;Ok, addition is commutative, right?&lt;br /&gt;{}+[] == 0&lt;br /&gt;And for luck: {}+{} == NaN&lt;br /&gt;As above, []+[] == &quot;&quot;&lt;br /&gt;** Actually, on playing with typeof, I think those are actually all strings.  &quot;[object Object]&quot;, &quot;0&quot;, &quot;NaN&quot;.  OTOH, {}+[4]+5 == 9 (but typeof string)&lt;br /&gt;** &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; 5+{}+[4]                                                        &lt;br /&gt;5[object Object]4  // because of course it does&lt;br /&gt;* &lt;br /&gt;&lt;pre&gt;
return
  x;
&lt;/pre&gt;&lt;br /&gt;turns into&lt;br /&gt;&lt;pre&gt;
 return;
 x;
&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;* All numbers are 64-bit floats; you can still bitshift them, but as integers, so 5.2 &amp;lt;&amp;lt; 2 == 20.  This makes more sense when I remembered that floats are weird, not integers+fractions, so a simple bitshift of the fraction wouldn&apos;t make sense.&lt;br /&gt;&lt;br /&gt;&lt;img src=&quot;https://www.dreamwidth.org/tools/commentcount?user=mindstalk&amp;ditemid=466385&quot; width=&quot;30&quot; height=&quot;12&quot; alt=&quot;comment count unavailable&quot; style=&quot;vertical-align: middle;&quot;/&gt; comments</description>
  <comments>https://mindstalk.dreamwidth.org/466385.html</comments>
  <category>javascript</category>
  <category>programming</category>
  <lj:security>public</lj:security>
  <lj:reply-count>0</lj:reply-count>
</item>
<item>
  <guid isPermaLink='true'>https://mindstalk.dreamwidth.org/465980.html</guid>
  <pubDate>Sun, 05 Feb 2017 16:49:15 GMT</pubDate>
  <title>Hoisting Shadows</title>
  <link>https://mindstalk.dreamwidth.org/465980.html</link>
  <description>A bit after writing &lt;a href=&quot;https://mindstalk.dreamwidth.org/465845.html&quot;&gt;the previous post on shadowing variables in JavaScript&lt;/a&gt;, I came across &lt;a href=&quot;http://www.w3schools.com/js/js_hoisting.asp&quot;&gt;this page on hoisting&lt;/a&gt;.  JavaScript re-writes your code so that declarations but not initializations to the top of current scope, meaning the script or function[1].  So&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;
console.log(a);
var a=5;
&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;turns into&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;
var a;
console.log(a);
a=5;
&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Put that way, it&apos;s clear why the problem happens, if not why the language was designed this way.&lt;br /&gt;&lt;br /&gt;Does the same thing happen in Python?  Searching did not get clear results.  I saw pages contrasting JavaScript with Python, which doesn&apos;t even *have* declarations.  OTOH, the same behavior occurs.  So... I dunno.&lt;br /&gt;&lt;br /&gt;[1] JavaScipt does not have block scoping the way C and Perl[2] do; scopes are delimited by curly braces, but random curly braces do nothing to isolate variables.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;
{ a=5; }
console.log(a);
&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;will work just fine. :(&lt;br /&gt;&lt;br /&gt;[2] As I was verifying this in Perl, I ran into behavior I&apos;d forgotten.  I&apos;d thrown in &quot;use strict;&quot; but wasn&apos;t getting the errors I expected.  Eventually I recalled that $a and $b have special meaning in Perl (I think for comparison functions), and I guess are pre-declared, and I was using $a a la the above code, so strict didn&apos;t complain about trying to access $a before assigning to it.  Sigh.&lt;br /&gt;&lt;br /&gt;&lt;img src=&quot;https://www.dreamwidth.org/tools/commentcount?user=mindstalk&amp;ditemid=465980&quot; width=&quot;30&quot; height=&quot;12&quot; alt=&quot;comment count unavailable&quot; style=&quot;vertical-align: middle;&quot;/&gt; comments</description>
  <comments>https://mindstalk.dreamwidth.org/465980.html</comments>
  <category>javascript</category>
  <category>programming</category>
  <category>python</category>
  <category>perl</category>
  <lj:security>public</lj:security>
  <lj:reply-count>0</lj:reply-count>
</item>
<item>
  <guid isPermaLink='true'>https://mindstalk.dreamwidth.org/465845.html</guid>
  <pubDate>Thu, 02 Feb 2017 18:43:58 GMT</pubDate>
  <title>Shadows of JavaScript</title>
  <link>https://mindstalk.dreamwidth.org/465845.html</link>
  <description>Months ago, Robbie had found this scoping problem in Python, which I &lt;a href=&quot;http://mindstalk.dreamwidth.org/450941.html&quot;&gt;reduced to essentials&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;I&apos;ve started finally learning JavaScript, and it has nicer lambdas than Python, and proper hiding of nested functions unlike Perl.  But it has the same scope problem:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;
g1 = 12;
function func() {
  document.getElementById(&quot;demo&quot;).innerHTML = g1;

  var g1 = 5;

}
func();
&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;(I&apos;m not including the HTML framework because DW/LJ would yell at me if I did.)&lt;br /&gt;&lt;br /&gt;Output is &apos;undefined&apos;, rather than 12.  As in Python, the local variable further down shadows the outer scope variable (doesn&apos;t matter if the &quot;g1=12&quot; has a &apos;var&apos; before it) even for lines before the local variable.&lt;br /&gt;&lt;br /&gt;As mentioned before, Perl has proper lexical scoping here (though not for nested functions.)  I don&apos;t think I can even create similar code in Scheme/Lisp, where the scoping is explicit with parentheses.  (There&apos;s &apos;define&apos; but I think that makes a new global, and it didn&apos;t work.)  In Ocaml I have&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;
let g1=&quot;10&quot;;;

let func () =
  print_endline g1;
  let g1=&quot;cat&quot; in
    g1
  ;;

func();;
&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Which I suspect is as explicit as Lisp parentheses, in its own way; the print line is obviously outside the following &quot;let ... in...&quot;.&lt;br /&gt;&lt;br /&gt;&lt;img src=&quot;https://www.dreamwidth.org/tools/commentcount?user=mindstalk&amp;ditemid=465845&quot; width=&quot;30&quot; height=&quot;12&quot; alt=&quot;comment count unavailable&quot; style=&quot;vertical-align: middle;&quot;/&gt; comments</description>
  <comments>https://mindstalk.dreamwidth.org/465845.html</comments>
  <category>ocaml</category>
  <category>javascript</category>
  <category>programming</category>
  <lj:security>public</lj:security>
  <lj:reply-count>0</lj:reply-count>
</item>
</channel>
</rss>
