javascript

Table of Contents

1. javascript

1.3. Javascript is a Mess

1.3.1. https://www.reddit.com/r/webdev/comments/dbmxso/why_is_the_javascript_environment_such_a_hot_mess/

  • Organic grow ⇒ inconsistencies and redundant code

JavaScript has grown organically with the web and as such is full of inconsistencies and redundant code. That’s makes it interesting but such a pain in the ass sometimes. PHP has evolved the same way and has the same qualities. Dynamic and vibrant but oh-so-infuriating. I go play with a python or rust project when I need a sanity check.

  • Need to keep backwards compatiblity with 10 years old browsers, each one with its own twists → Babel

Most other languages operate in a more controlled ecosystem and don’t have to worry about being compatible with 10 year old browsers on every platform.

1.3.2. https://dev.to/sotiriskourouklis/why-javascript-is-a-mess-25j2

  • Lack of consistency. There are many ways to do the same thing, many folder structures, code style, …
    • For example

1.3.4. Is JavaScript A Hot Mess?

1.3.4.1. 1:33

Small dependencies added to a lot of projects when copy-paste would have been better:
https://qz.com/646467/how-one-programmer-broke-the-internet-by-deleting-a-tiny-piece-of-code/

1.3.4.2. 2:02

Created in 10 days(!), browser fragmentation

1.3.4.3. 3:38

node_modules is huge and has a lot of packages seem overcomplicated

1.3.4.4. 5:27

Complicated builds

  • Babel
  • Webpack
1.3.4.5. 6:02

CSS preprocessors (also complex builds)

1.3.4.6. 6:17

A lot of loaders with different configuration each

1.3.4.7. 6:33

You could probably get rid of half of the tools

1.3.4.8. 7:23

Fast pace of changes means if you’re out of javascript for 4 years ⇒ outdated

1.3.4.9. 8:18

Starter code packs are HUGE because there is a lot of different tooling/build/code formater and they just bundle it with the start code so that everything works
This leads to huge Hello World examples, and only when you start to understand all of this you actually make smaller apps

1.3.4.10. 9:56

Dynamic typing with automatic conversion ⇒ weird rules that you don’t expect

1.5. Web Assembly wasm

1.5.1. The Birth & Death of JavaScript

Treat Javascript as another platform (like x86, ARM), by using transpilers
https://www.destroyallsoftware.com/talks/the-birth-and-death-of-javascript

1.5.5. WASMtoy

:ID: 0370ff02-a942-4ffc-8353-2d289dcf1005

1.5.7. Python wasm

pip install nuitka # necesita patchelf y ccache opcionalmente para que vaya más rápido
time CC=clang MAKEFLAGS="-j8" python -m nuitka --follow-imports  script.py -o script.elf
pip install py2wasm # Hay que instalar wasmer luego para hacerlo funcionar
# MAKEFLAGS no hace que wasm-ld utilize todos los núcleos
MAKEFLAGS="-j8" py2wasm script.py -o script.wasm

1.6. npm

https://registry.npmjs.org/<name of package> → search tarball:
https://registry.npmjs.org/@hokyjack/jupyterlab-monokai-plus/-/jupyterlab-monokai-plus-0.1.4.tgz

  • https://deb.nodesource.com/setup_16.x Para instalar node curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -. Qué podría salir mal? ¯\_(ツ)_/¯
  • Luego he tenido que instalar nvm: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

1.6.1. https://github.com/Schniz/fnm   try

🚀 Fast and simple Node.js version manager, built in Rust

1.9. Aprender JS   learn

https://javascript.info/
https://hackr.io/blog/best-javascript-courses Avanzado
“A Study Plan To Cure JavaScript Fatigue” por Sacha Greif
https://link.medium.com/9lipfoF0W8

1.9.1. Libros

  • Javascript for Impatient Programmers
  • Eloquent Javascript

1.9.2. Trucos

  • Object.keys(objeto) ,Object.values(objeto) y Object.entries(objeto) para sacar las claves, valores y entradas (items en Python) de un objeto
  • Array.from(objeto) para convertir lo que sea en array y poder utilizar .map
  • Quitar clave de un diccionario

      delete thisIsObject['Cow'];
    

    If you are using Underscore.js or Lodash, there is a function ’omit’ that will do it.
    http://underscorejs.org/#omit

  • Create an object within a map function

      const keys = ['key1', 'key2'];
    
      const result = keys.map(key => {
        let obj = {};
        obj[key] = (Math.random() * 2) - 1;
        return obj;
      });
      return result;
    

    [ { key1: 0.45488502758210236 }, { key2: 0.4172403939051943 } ]

  • Slice
    https://www.freecodecamp.org/news/how-to-remove-an-element-from-a-javascript-array-removing-a-specific-item-in-js/

      const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
      var n = 3
      console.log( arrayOfLetters.slice(0, n).concat(arrayOfLetters.slice(n+1)) )
      n = -1
      console.log( arrayOfLetters.slice(0, n) )
      console.log( arrayOfLetters.slice(1) )
    

    [ ’a’, ’b’, ’c’, ’e’, ’f’ ]
    [ ’a’, ’b’, ’c’, ’d’, ’e’ ]
    [ ’b’, ’c’, ’d’, ’e’, ’f’ ]
    undefined

  • Object.getPrototypeOf para obtener la clase de un objeto (typeof no vale)
  • Para comprobar si está una string en un array

      Object.keys(data).includes("next")
    
  • Defaultdict en javascript

      var defaultDict = new Proxy({}, {
        get: (target, name) => name in target ? target[name] : 0
      })
    
  • pivot_table

      conteo_resultados = resultados
        .map((x) => {
          let obj = {};
          // Convertir de {"estado": "Activo", "estado__count": 1} a {"Activo": 1}
          obj[x["estado"]] = x["estado__count"];
          return obj;
        })
        .reduce((acc, obj) => {
          // Juntar todos {"Activo": 1, "Inactivo": 2}
          Object.keys(obj).forEach((key) => ((acc[key] || 0) + obj[key]);
          return acc;
        });
    
  • range() function
    […Array(5).keys()];
  • JavaScript Date Reference
    No tiene un strftime/strptime sin librerías
  • urlencode
    encodeURIComponent

1.9.4. Reemplazar página web

Con JQuery

$(document).ready(function() {
  // Replace the current window with the contents from a new URL
  function replaceWindowWithContent(url) {
    $.get(url, function(data) {
      $('body').html(data);
      window.history.replaceState(null, '', url);
    });
  }

  // Example usage: Replace the window with contents from a new URL on button click
  $('#replaceButton').on('click', function() {
    var newURL = 'https://example.com';  // Replace with your desired URL
    replaceWindowWithContent(newURL);
  });
});

1.9.7. Typescript

1.9.7.1. Typescript typing limitations

1.9.10. “7 Must-Use Developer Tools for Increased Efficiency”

“7 Must-Use Developer Tools for Increased Efficiency” por Mahdhi Rezvi
https://link.medium.com/RfwZLBnh88 Bit.dev interesante para ver ejemplos de código

1.9.12. Lista de tecnologías

https://guillaumechereau.github.io/js-is-a-mess/

  • angular
  • babel
  • backbone
  • bootstrap
  • bower
  • browserify
  • closure compiler
  • ember
  • grunt
  • gulp
  • handlebars
  • jquery
  • jquery ui
  • jsx
  • less
  • materializecss
  • meteor
  • modernizr
  • mustache
  • nodejs
  • npm
  • polymer
  • react
  • sass
  • stylus
  • underscore
  • vuejs
  • webpack
  • yeoman

1.10. Dokuwiki

1.10.1. </> htmx - high power tools for html → HTML as complete hypertext

  • Why should only <a> and <form> be able to make HTTP requests?
  • Why should only click & submit events trigger them?
  • Why should only GET & POST methods be available?
  • Why should you only be able to replace the entire screen?
    By removing these arbitrary constraints, htmx completes HTML as a hypertext
  • </> htmx ~ When Should You Use Hypermedia?
    • Hypermedia: A Good Fit If…
      • …If your UI is mostly text & images
      • …If your UI is CRUD-y
      • …If your UI is “nested”, with updates mostly taking place within well-defined blocks
      • …If you need “deep links” & good first-render performance
    • Hypermedia: Not A Good Fit If…
      • #…If your UI has many, dynamic interdependencies
      • …If you require offline functionality
      • …If your UI state is updated extremely frequently
      • …If your team is not on board
  • Django, HTMX and Alpine.JS: A Match Made In Heaven - DEV Community

Author: Julian Lopez Carballal

Created: 2024-09-16 Mon 05:47