<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dw="https://www.dreamwidth.org">
  <id>tag:dreamwidth.org,2009-05-20:374172</id>
  <title>Rich and Strange Aeons</title>
  <subtitle>mindstalk</subtitle>
  <author>
    <name>mindstalk</name>
  </author>
  <link rel="alternate" type="text/html" href="https://mindstalk.dreamwidth.org/"/>
  <link rel="self" type="text/xml" href="https://mindstalk.dreamwidth.org/data/atom"/>
  <updated>2022-02-04T02:17:10Z</updated>
  <dw:journal username="mindstalk" type="personal"/>
  <entry>
    <id>tag:dreamwidth.org,2009-05-20:374172:570790</id>
    <link rel="alternate" type="text/html" href="https://mindstalk.dreamwidth.org/570790.html"/>
    <link rel="self" type="text/xml" href="https://mindstalk.dreamwidth.org/data/atom/?itemid=570790"/>
    <title>more Javascript</title>
    <published>2020-10-22T21:34:12Z</published>
    <updated>2022-02-04T02:17:10Z</updated>
    <category term="programming"/>
    <category term="javascript"/>
    <dw:security>public</dw:security>
    <dw:reply-count>0</dw:reply-count>
    <content type="html">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't found.)  Like 'var' variables have function scope (useful concept; I think it applies to Python), but you can get block scope (One True Way) by using 'let' instead.  I don't think I knew that simply assigning to an undeclared variable creates a global (dear gods) but JS borrowed 'use strict'; 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.  "for in" 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 'for in' 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'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 'v[0]' thinking I was using 'for of" (which iterates over the values of an Array) when I wasn't, and I got numbers rather than letters or 'undefined', 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="https://www.dreamwidth.org/tools/commentcount?user=mindstalk&amp;ditemid=570790" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/&gt; comments</content>
  </entry>
  <entry>
    <id>tag:dreamwidth.org,2009-05-20:374172:564767</id>
    <link rel="alternate" type="text/html" href="https://mindstalk.dreamwidth.org/564767.html"/>
    <link rel="self" type="text/xml" href="https://mindstalk.dreamwidth.org/data/atom/?itemid=564767"/>
    <title>JS surprise</title>
    <published>2020-07-15T02:39:41Z</published>
    <updated>2020-07-15T02:39:41Z</updated>
    <category term="javascript"/>
    <category term="programming"/>
    <dw:security>public</dw:security>
    <dw:reply-count>0</dw:reply-count>
    <content type="html">I was looking at my 'programming' 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'd asked me yesterday if I'd ever taught myself JavaScript I would have said 'no', 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't taught myself JS...&lt;br /&gt;&lt;br /&gt;&lt;img src="https://www.dreamwidth.org/tools/commentcount?user=mindstalk&amp;ditemid=564767" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/&gt; comments</content>
  </entry>
  <entry>
    <id>tag:dreamwidth.org,2009-05-20:374172:466385</id>
    <link rel="alternate" type="text/html" href="https://mindstalk.dreamwidth.org/466385.html"/>
    <link rel="self" type="text/xml" href="https://mindstalk.dreamwidth.org/data/atom/?itemid=466385"/>
    <title>JavaScript quirks</title>
    <published>2017-02-05T17:35:25Z</published>
    <updated>2020-10-23T03:27:27Z</updated>
    <category term="programming"/>
    <category term="javascript"/>
    <dw:security>public</dw:security>
    <dw:reply-count>0</dw:reply-count>
    <content type="html">I've started studying JavaScript a bit, as you might guess from recent posts.  It's struck me as a mix of Perl, Python, and crack.  It's got some neat things, especially in comparison to one language or the other.  And it's got lots of... wtf things.&lt;br /&gt;&lt;br /&gt;+: exponentiation operator; nested arrays and objects (dictionaries) (without Perl's references); first class functions and lambdas and closures, including nested functions (unlike Perl); Perl-like push/pop/shift/unshift array operators (but what's the performance?); consistent 'valueOf' and 'toString' 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'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'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't.  [2020 edit: what was I thinking?  string.slice can take negative indices, but character access via [] doesn't.  And array.slice also takes negative.]&lt;br /&gt;* there's a typeof operator, but it just says 'object' for arrays.&lt;br /&gt;* "5"+2 == "52" (convert 2 to "2", concatenate), but "5"-2 == 3 (convert "5" to 5, subtract.) And no Python string multiplier like "a"*2 == "aa".&lt;br /&gt;** As Avi noted, it gets even weirder given that the values could be hidden in a variable.  a+2=="52", a-2==3&lt;br /&gt;* [1,2]+[3,4] doesn't concatenate arrays, doesn't add by element, doesn't give a type error, but gives... "1,23,4" (turn arrays into strings, concatenate without delimiter.)&lt;br /&gt;&lt;br /&gt;My friend Mark linked me to &lt;a href="https://www.destroyallsoftware.com/talks/wat"&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, []+[] == ""&lt;br /&gt;** Actually, on playing with typeof, I think those are actually all strings.  "[object Object]", "0", "NaN".  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't make sense.&lt;br /&gt;&lt;br /&gt;&lt;img src="https://www.dreamwidth.org/tools/commentcount?user=mindstalk&amp;ditemid=466385" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/&gt; comments</content>
  </entry>
  <entry>
    <id>tag:dreamwidth.org,2009-05-20:374172:465980</id>
    <link rel="alternate" type="text/html" href="https://mindstalk.dreamwidth.org/465980.html"/>
    <link rel="self" type="text/xml" href="https://mindstalk.dreamwidth.org/data/atom/?itemid=465980"/>
    <title>Hoisting Shadows</title>
    <published>2017-02-05T16:49:15Z</published>
    <updated>2017-02-05T16:49:15Z</updated>
    <category term="python"/>
    <category term="javascript"/>
    <category term="perl"/>
    <category term="programming"/>
    <dw:security>public</dw:security>
    <dw:reply-count>0</dw:reply-count>
    <content type="html">A bit after writing &lt;a href="https://mindstalk.dreamwidth.org/465845.html"&gt;the previous post on shadowing variables in JavaScript&lt;/a&gt;, I came across &lt;a href="http://www.w3schools.com/js/js_hoisting.asp"&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'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'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'd forgotten.  I'd thrown in "use strict;" but wasn'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't complain about trying to access $a before assigning to it.  Sigh.&lt;br /&gt;&lt;br /&gt;&lt;img src="https://www.dreamwidth.org/tools/commentcount?user=mindstalk&amp;ditemid=465980" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/&gt; comments</content>
  </entry>
  <entry>
    <id>tag:dreamwidth.org,2009-05-20:374172:465845</id>
    <link rel="alternate" type="text/html" href="https://mindstalk.dreamwidth.org/465845.html"/>
    <link rel="self" type="text/xml" href="https://mindstalk.dreamwidth.org/data/atom/?itemid=465845"/>
    <title>Shadows of JavaScript</title>
    <published>2017-02-02T18:43:58Z</published>
    <updated>2017-02-02T18:43:58Z</updated>
    <category term="ocaml"/>
    <category term="javascript"/>
    <category term="programming"/>
    <dw:security>public</dw:security>
    <dw:reply-count>0</dw:reply-count>
    <content type="html">Months ago, Robbie had found this scoping problem in Python, which I &lt;a href="http://mindstalk.dreamwidth.org/450941.html"&gt;reduced to essentials&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;I'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("demo").innerHTML = g1;

  var g1 = 5;

}
func();
&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;(I'm not including the HTML framework because DW/LJ would yell at me if I did.)&lt;br /&gt;&lt;br /&gt;Output is 'undefined', rather than 12.  As in Python, the local variable further down shadows the outer scope variable (doesn't matter if the "g1=12" has a 'var' 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't think I can even create similar code in Scheme/Lisp, where the scoping is explicit with parentheses.  (There's 'define' but I think that makes a new global, and it didn't work.)  In Ocaml I have&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;
let g1="10";;

let func () =
  print_endline g1;
  let g1="cat" 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 "let ... in...".&lt;br /&gt;&lt;br /&gt;&lt;img src="https://www.dreamwidth.org/tools/commentcount?user=mindstalk&amp;ditemid=465845" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/&gt; comments</content>
  </entry>
</feed>
