Advertisement
Guest User

How it feels to learn JavaScript in 2016

a guest
Oct 4th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.87 KB | None | 0 0
  1. Hey, I got this new web project, but to be honest I haven’t coded much web in a few years and I’ve heard the landscape changed a bit. You are the most up-to date web dev around here right?
  2.  
  3. -The actual term is Front End engineer, but yeah, I’m the right guy. I do web in 2016. Visualisations, music players, flying drones that play football, you name it. I just came back from JsConf and ReactConf, so I know the latest technologies to create web apps.
  4.  
  5. Cool. I need to create a page that displays the latest activity from the users, so I just need to get the data from the REST endpoint and display it in some sort of filterable table, and update it if anything changes in the server. I was thinking maybe using jQuery to fetch and display the data?
  6.  
  7. -Oh my god no, no one uses jQuery anymore. You should try learning React, it’s 2016.
  8.  
  9. Oh, OK. What’s React?
  10.  
  11. -It’s a super cool library made by some guys at Facebook, it really brings control and performance to your application, by allowing you to handle any view changes very easily.
  12.  
  13. That sounds neat. Can I use React to display data from the server?
  14.  
  15. -Yeah, but first you need to add React and React DOM as a library in your webpage.
  16.  
  17. Wait, why two libraries?
  18.  
  19. -So one is the actual library and the second one is for manipulating the DOM, which now you can describe in JSX.
  20.  
  21. JSX? What is JSX?
  22.  
  23. -JSX is just a JavaScript syntax extension that looks pretty much like XML. It’s kind of another way to describe the DOM, think of it as a better HTML.
  24.  
  25. What’s wrong with HTML?
  26.  
  27. -It’s 2016. No one codes HTML directly anymore.
  28.  
  29. Right. Anyway, if I add these two libraries then I can use React?
  30.  
  31. -Not quite. You need to add Babel, and then you are able to use React.
  32.  
  33. Another library? What’s Babel?
  34.  
  35. -Oh, Babel is a transpiler that allows you to target specific versions of JavaScript, while you code in any version of JavaScript. You don’t HAVE to include Babel to use ReactJS, but unless you do, you are stuck with using ES5, and let’s be real, it’s 2016, you should be coding in ES2016+ like the rest of the cool kids do.
  36.  
  37. ES5? ES2016+? I’m getting lost over here. What’s ES5 and ES2016+?
  38.  
  39. -ES5 stands for ECMAScript 5. It’s the edition that has most people target since it has been implemented by most browsers nowadays.
  40.  
  41. ECMAScript?
  42.  
  43. -Yes, you know, the scripting standard JavaScript was based on in 1999 after its initial release in 1995, back then when JavaScript was named Livescript and only ran in the Netscape Navigator. That was very messy back then, but thankfully now things are very clear and we have, like, 7 editions of this implementation.
  44.  
  45. 7 editions. For real. And ES5 and ES2016+ are?
  46.  
  47. -The fifth and seventh edition respectively.
  48.  
  49. Wait, what happened with the sixth?
  50.  
  51. -You mean ES6? Yeah, I mean, each edition is a superset of the previous one, so if you are using ES2016+, you are using all the features of the previous versions.
  52.  
  53. Right. And why use ES2016+ over ES6 then?
  54.  
  55. -Well, you COULD use ES6, but to use cool features like async and await, you need to use ES2016+. Otherwise you are stuck with ES6 generators with coroutines to block asynchronous calls for proper control flow.
  56.  
  57. I have no idea what you just said, and all these names are confusing. Look, I’m just loading a bunch of data from a server, I used to be able to just include jQuery from a CDN and just get the data with AJAX calls, why can’t I just do that?
  58.  
  59. -It’s 2016 man, no one uses jQuery anymore, it ends up in a bunch of spaghetti code. Everyone knows that.
  60.  
  61. Right. So my alternative is to load three libraries to fetch data and display a HTML table.
  62.  
  63. -Well, you include those three libraries but bundle them up with a module manager to load only one file.
  64.  
  65. I see. And what’s a module manager?
  66.  
  67. -The definition depends on the environment, but in the web we usually mean anything that supports AMD or CommonJS modules.
  68.  
  69. Riiight. And AMD and CommonJS are…?
  70.  
  71. -Definitions. There are ways to describe how multiple JavaScript libraries and classes should interact. You know, exports and requires? You can write multiple JavaScript files defining the AMD or CommonJS API and you can use something like Browserify to bundle them up.
  72.  
  73. OK, that makes sense… I think. What is Browserify?
  74.  
  75. -It’s a tool that allows you to bundle CommonJS described dependencies to files that can be run in the browser. It was created because most people publish those dependencies in the npm registry.
  76.  
  77. npm registry?
  78.  
  79. -It’s a very big public repository where smart people put code and dependencies as modules.
  80.  
  81. Like a CDN?
  82.  
  83. -Not really. It’s more like a centralised database where anyone can publish and download libraries, so you can use them locally for development and then upload them to a CDN if you want to.
  84.  
  85. Oh, like Bower!
  86.  
  87. -Yes, but it’s 2016 now, no one uses Bower anymore.
  88.  
  89. Oh, I see… so I need to download the libraries from npm then?
  90.  
  91. -Yes. So for instance, if you want to use React , you download the React module and import it in your code. You can do that for almost every popular JavaScript library.
  92.  
  93. Oh, like Angular!
  94.  
  95. -Angular is so 2015. But yes. Angular would be there, alongside VueJS or RxJS and other cool 2016 libraries. Want to learn about those?
  96.  
  97. Let’s stick with React, I’m already learning too many things now. So, if I need to use React I fetch it from this npm and then use this Browserify thing?
  98.  
  99. -Yes.
  100.  
  101. That seems overly complicated to just grab a bunch of dependencies and tie them together.
  102.  
  103. -It is, that’s why you use a task manager like Grunt or Gulp or Broccoli to automate running Browserify. Heck, you can even use Mimosa.
  104.  
  105. Grunt? Gulp? Broccoli? Mimosa? The heck are we talking about now?
  106.  
  107. -Task managers. But they are not cool anymore. We used them in like, 2015, then we used Makefiles, but now we wrap everything with Webpack.
  108.  
  109. Makefiles? I thought that was mostly used on C or C++ projects.
  110.  
  111. -Yeah, but apparently in the web we love making things complicated and then going back to the basics. We do that every year or so, just wait for it, we are going to do assembly in the web in a year or two.
  112.  
  113. Sigh. You mentioned something called Webpack?
  114.  
  115. -It’s another module manager for the browser while being kind of a task runner as well. It’s like a better version of Browserify.
  116.  
  117. Oh, Ok. Why is it better?
  118.  
  119. -Well, maybe not better, it’s just more opinionated on how your dependencies should be tied. Webpack allows you to use different module managers, and not only CommonJS ones, so for instance native ES6 supported modules.
  120.  
  121. I’m extremely confused by this whole CommonJS/ES6 thing.
  122.  
  123. -Everyone is, but you shouldn’t care anymore with SystemJS.
  124.  
  125. Jesus christ, another noun-js. Ok, and what is this SystemJS?
  126.  
  127. -Well, unlike Browserify and Webpack 1.x, SystemJS is a dynamic module loader that allows you to tie multiple modules in multiple files instead of bundling them in one big file.
  128.  
  129. Wait, but I thought we wanted to build our libraries in one big file and load that!
  130.  
  131. -Yes, but because HTTP/2 is coming now multiple HTTP requests are actually better.
  132.  
  133. Wait, so can’t we just add the three original libraries for React??
  134.  
  135. -Not really. I mean, you could add them as external scripts from a CDN, but you would still need to include Babel then.
  136.  
  137. Sigh. And that is bad right?
  138.  
  139. -Yes, you would be including the entire babel-core, and it wouldn’t be efficient for production. On production you need to perform a series of pre-tasks to get your project ready that make the ritual to summon Satan look like a boiled eggs recipe. You need to minify assets, uglify them, inline css above the fold, defer scripts, as well as-
  140.  
  141. I got it, I got it. So if you wouldn’t include the libraries directly in a CDN, how would you do it?
  142.  
  143. -I would transpile it from Typescript using a Webpack + SystemJS + Babel combo.
  144.  
  145. Typescript? I thought we were coding in JavaScript!
  146.  
  147. -Typescript IS JavaScript, or better put, a superset of JavaScript, more specifically JavaScript on version ES6. You know, that sixth version we talked about before?
  148.  
  149. I thought ES2016+ was already a superset of ES6! WHY we need now this thing called Typescript?
  150.  
  151. -Oh, because it allows us to use JavaScript as a typed language, and reduce run-time errors. It’s 2016, you should be adding some types to your JavaScript code.
  152.  
  153. And Typescript obviously does that.
  154.  
  155. -Flow as well, although it only checks for typing while Typescript is a superset of JavaScript which needs to be compiled.
  156.  
  157. Sigh… and Flow is?
  158.  
  159. -It’s a static type checker made by some guys at Facebook. They coded it in OCaml, because functional programming is awesome.
  160.  
  161. OCaml? Functional programming?
  162.  
  163. -It’s what the cool kids use nowadays man, you know, 2016? Functional programming? High order functions? Currying? Pure functions?
  164.  
  165. I have no idea what you just said.
  166.  
  167. -No one does at the beginning. Look, you just need to know that functional programming is better than OOP and that’s what we should be using in 2016.
  168.  
  169. Wait, I learned OOP in college, I thought that was good?
  170.  
  171. -So was Java before being bought by Oracle. I mean, OOP was good back in the days, and it still has its uses today, but now everyone is realising modifying states is equivalent to kicking babies, so now everyone is moving to immutable objects and functional programming. Haskell guys had been calling it for years, -and don’t get me started with the Elm guys- but luckily in the web now we have libraries like Ramda that allow us to use functional programming in plain JavaScript.
  172.  
  173. Are you just dropping names for the sake of it? What the hell is Ramnda?
  174.  
  175. -No. Ramda. Like Lambda. You know, that David Chambers’ library?
  176.  
  177. David who?
  178.  
  179. -David Chambers. Cool guy. Plays a mean Coup game. One of the contributors for Ramda. You should also check Erik Meijer if you are serious about learning functional programming.
  180.  
  181. And Erik Meijer is…?
  182.  
  183. -Functional programming guy as well. Awesome guy. He has a bunch of presentations where he trashes Agile while using this weird coloured shirt. You should also check some of the stuff from Tj, Jash Kenas, Sindre Sorhus, Paul Irish, Addy Osmani-
  184.  
  185. Ok. I’m going to stop you there. All that is good and fine, but I think all that is just so complicated and unnecessary for just fetching data and displaying it. I’m pretty sure I don’t need to know these people or learn all those things to create a table with dynamic data. Let’s get back to React. How can I fetch the data from the server with React?
  186.  
  187. -Well, you actually don’t fetch the data with React, you just display the data with React.
  188.  
  189. Oh, damn me. So what do you use to fetch the data?
  190.  
  191. -You use Fetch to fetch the data from the server.
  192.  
  193. I’m sorry? You use Fetch to fetch the data? Whoever is naming those things needs a thesaurus.
  194.  
  195. -I know right? Fetch it’s the name of the native implementation for performing XMLHttpRequests against a server.
  196.  
  197. Oh, so AJAX.
  198.  
  199. -AJAX is just the use of XMLHttpRequests. But sure. Fetch allows you to do AJAX based in promises, which then you can resolve to avoid the callback hell.
  200.  
  201. Callback hell?
  202.  
  203. -Yeah. Every time you perform an asynchronous request against the server, you need to wait for its response, which then makes you to add a function within a function, which is called the callback pyramid from hell.
  204.  
  205. Oh, Ok. And this promise thing solves it?
  206.  
  207. -Indeed. By manipulating your callbacks through promises, you can write easier to understand code, mock and test them, as well as perform simultaneous requests at once and wait until all of them are loaded.
  208.  
  209. And that can be done with Fetch?
  210.  
  211. -Yes, but only if your user uses an evergreen browser, otherwise you need to include a Fetch polyfill or use Request, Bluebird or Axios.
  212.  
  213. How many libraries do I need to know for god’s sake? How many are of them?
  214.  
  215. -It’s JavaScript. There has to be thousands of libraries that all do the same thing. We know libraries, in fact, we have the best libraries. Our libraries are huuuge, and sometimes we include pictures of Guy Fieri in them.
  216.  
  217. Did you just say Guy Fieri? Let’s get this over with. What these Bluebird, Request, Axios libraries do?
  218.  
  219. -They are libraries to perform XMLHttpRequests that return promises.
  220.  
  221. Didn’t jQuery’s AJAX method start to return promises as well?
  222.  
  223. -We don’t use the “J” word in 2016 anymore. Just use Fetch, and polyfill it when it’s not in a browser or use Bluebird, Request or Axios instead. Then manage the promise with await within an async function and boom, you have proper control flow.
  224.  
  225. It’s the third time you mention await but I have no idea what it is.
  226.  
  227. -Await allows you to block an asynchronous call, allowing you to have better control on when the data is being fetch and overall increasing code readability. It’s awesome, you just need to make sure you add the stage-3 preset in Babel, or use syntax-async-functions and transform-async-to-generator plugin.
  228.  
  229. This is insane.
  230.  
  231. -No, insane is the fact you need to precompile Typescript code and then transpile it with Babel to use await.
  232.  
  233. Wat? It’s not included in Typescript?
  234.  
  235. -It does in the next version, but as of version 1.7 it only targets ES6, so if you want to use await in the browser, first you need to compile your Typescript code targeting ES6 and then Babel that shit up to target ES5.
  236.  
  237. At this point I don’t know what to say.
  238.  
  239. -Look, it’s easy. Code everything in Typescript. All modules that use Fetch compile them to target ES6, transpile them with Babel on a stage-3 preset, and load them with SystemJS. If you don’t have Fetch, polyfill it, or use Bluebird, Request or Axios, and handle all your promises with await.
  240.  
  241. We have very different definitions of easy. So, with that ritual I finally fetched the data and now I can display it with React right?
  242.  
  243. -Is your application going to handle any state changes?
  244.  
  245. Err, I don’t think so. I just need to display the data.
  246.  
  247. -Oh, thank god. Otherwise I would had to explain you Flux, and implementations like Flummox, Alt, Fluxible. Although to be honest you should be using Redux.
  248.  
  249. I’m going to just fly over those names. Again, I just need to display data.
  250.  
  251. -Oh, if you are just displaying the data you didn’t need React to begin with. You would had been fine with a templating engine.
  252.  
  253. Are you kidding me? Do you think this is funny? Is that how you treat your loved ones?
  254.  
  255. -I was just explaining what you could use.
  256.  
  257. Stop. Just stop.
  258.  
  259. -I mean, even if it’s just using templating engine, I would still use a Typescript + SystemJS + Babel combo if I were you.
  260.  
  261. I need to display data on a page, not perform Sub Zero’s original MK fatality. Just tell me what templating engine to use and I’ll take it from there.
  262.  
  263. -There’s a lot, which one you are familiar with?
  264.  
  265. Ugh, can’t remember the name. It was a long time ago.
  266.  
  267. -jTemplates? jQote? PURE?
  268.  
  269. Err, doesn’t ring a bell. Another one?
  270.  
  271. -Transparency? JSRender? MarkupJS? KnockoutJS? That one had two-way binding.
  272.  
  273. Another one?
  274.  
  275. -PlatesJS? jQuery-tmpl? Handlebars? Some people still use it.
  276.  
  277. Maybe. Are there similar to that last one?
  278.  
  279. -Mustache, underscore? I think now even lodash has one to be honest, but those are kind of 2014.
  280.  
  281. Err.. maybe it was newer.
  282.  
  283. -Jade? DustJS?
  284.  
  285. No.
  286.  
  287. -DotJS? EJS?
  288.  
  289. No.
  290.  
  291. -Nunjucks? ECT?
  292.  
  293. No.
  294.  
  295. -Mah, no one likes Coffeescript syntax anyway. Jade?
  296.  
  297. No, you already said Jade.
  298.  
  299. -I meant Pug. I meant Jade. I mean, Jade is now Pug.
  300.  
  301. Sigh. No. Can’t remember. Which one would you use?
  302.  
  303. -Probably just ES6 native template strings.
  304.  
  305. Let me guess. And that requires ES6.
  306.  
  307. -Correct.
  308.  
  309. Which, depending on what browser I’m using needs Babel.
  310.  
  311. -Correct.
  312.  
  313. Which, if I want to include without adding the entire core library, I need to load it as a module from npm.
  314.  
  315. -Correct.
  316.  
  317. Which, requires Browserify, or Wepback, or most likely that other thing called SystemJS.
  318.  
  319. -Correct.
  320.  
  321. Which, unless it’s Webpack, ideally should be managed by a task runner.
  322.  
  323. -Correct.
  324.  
  325. But, since I should be using functional programming and typed languages I first need to pre-compile Typescript or add this Flow thingy.
  326.  
  327. -Correct.
  328.  
  329. And then send that to Babel if I want to use await.
  330.  
  331. -Correct.
  332.  
  333. So I can then use Fetch, promises, and control flow and all that magic.
  334.  
  335. -Just don’t forget to polyfill Fetch if it’s not supported, Safari still can’t handle it.
  336.  
  337. You know what. I think we are done here. Actually, I think I’m done. I’m done with the web, I’m done with JavaScript altogether.
  338.  
  339. -That’s fine, in a few years we all are going to be coding in Elm or WebAssembly.
  340.  
  341. I’m just going to move back to the backend. I just can’t handle these many changes and versions and editions and compilers and transpilers. The JavaScript community is insane if it thinks anyone can keep up with this.
  342.  
  343. -I hear you. You should try the Python community then.
  344.  
  345. Why?
  346.  
  347. -Ever heard of Python 3?
  348.  
  349. // Saved from https://hackernoon.com/how-it-feels-to-learn-javascript-in-2016-d3a717dd577f
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement