Live data from Hacker News

A little bit of plain JavaScript can do a lot

jvns.ca

11–20 of 206 posts

Re: A little bit of plain JavaScript can do a lot

#11

I agree about the nuisance of creating DOM elements. innerHTML is OK if you’re doing static content, but for anything that needs to be dynamic (untrusted input, event handlers, etc.) I have a little tiny helper library that I carry around in my head and write into projects that need it: const $T = text => document.createTextNode(text) function $E(tag, props, kids) { const elem = document.createElement(tag) for (const…

$E is pretty close to React.createElement, which JSX's tags compile to

https://reactjs.org/docs/react-without-jsx.html

Re: A little bit of plain JavaScript can do a lot

#12

It’s a little funny seeing vue/angular/react/whatever people discover vanilla JS. I thought classList, innerHTML, and querySelectorAll were all extremely common knowledge.

If I'm not mistaken, she's a systems programmer. More low-level stuff. She's definitely not a frontend/React/vue.js dev, as she says in the article.

She's written some excellent articles on lower-level programming.

But I agree that VanillaJS is not appreciated by a large number of web developers.

Re: A little bit of plain JavaScript can do a lot

#13

More specifically, how JavaScript can do a lot in browser by interacting through the DOM API. The modernization of the DOM API in the last 5 years has done a lot to remove the need for jQuery et al, and has made building the View part of JavaScript apps much more frictionless.

Where can we learn about this “modern JS“? I feel like in some ways I’m learning the old ways, similar to old c++.

It's both modern Web APIs as well as ES6 JavaScript, both of which make working in VanillaJS quite pleasant.

If you don't need to support older browsers, you don't even need to use any packaging software (although if you're building a serious app, it's still wise to do so). You can import ES6 modules in vanilla JS, and a modern browser will do the loading for you. Great for quick starts.

Re: A little bit of plain JavaScript can do a lot

#15

I agree about the nuisance of creating DOM elements. innerHTML is OK if you’re doing static content, but for anything that needs to be dynamic (untrusted input, event handlers, etc.) I have a little tiny helper library that I carry around in my head and write into projects that need it: const $T = text => document.createTextNode(text) function $E(tag, props, kids) { const elem = document.createElement(tag) for (const…

This is fairly similar to hyperscript: https://github.com/hyperhype/hyperscript/blob/master/README....

Re: A little bit of plain JavaScript can do a lot

#17
post #11

I agree about the nuisance of creating DOM elements. innerHTML is OK if you’re doing static content, but for anything that needs to be dynamic (untrusted input, event handlers, etc.) I have a little tiny helper library that I carry around in my head and write into projects that need it: const $T = text => document.createTextNode(text) function $E(tag, props, kids) { const elem = document.createElement(tag) for (const…

$E is pretty close to React.createElement, which JSX's tags compile to https://reactjs.org/docs/react-without-jsx.html

I’m not surprised by the convergent evolution—it’s the obvious API for creating elements. (My earliest versions of $E had arguments (tag, kids, fn) and would call fn(elem) so it could perform arbitrary modifications on the node, but I eventually realized that all I ever did was set properties and it was silly to have an entire lambda just for that.)

Re: A little bit of plain JavaScript can do a lot

#18

More specifically, how JavaScript can do a lot in browser by interacting through the DOM API. The modernization of the DOM API in the last 5 years has done a lot to remove the need for jQuery et al, and has made building the View part of JavaScript apps much more frictionless.

You still end up re-implementing half of jQuery. Because element creation is just as much passion as it was in 1999. Because useful functions are limited or stunted compared to jQuery counterparts (querySelectorAll returns a weird object instead of an array and throws exceptions if you as as much as as look at it funny, etc.).

Re: A little bit of plain JavaScript can do a lot

#19

I agree about the nuisance of creating DOM elements. innerHTML is OK if you’re doing static content, but for anything that needs to be dynamic (untrusted input, event handlers, etc.) I have a little tiny helper library that I carry around in my head and write into projects that need it: const $T = text => document.createTextNode(text) function $E(tag, props, kids) { const elem = document.createElement(tag) for (const…

Using tagged template literals the way lit-html does is the nicest JSX-substitute I’ve seen: https://github.com/Polymer/lit-html

Re: A little bit of plain JavaScript can do a lot

#20
Instead of HTML, can’t JavaScript just be used to paint the browser canvas?

You can create your text boxes, your drop downs, buttons, etc., everything that makes it a GUI application.

Then you fetch your data, per the page you display, via JSON, and fill in the fields.

The initial JavaScript download is heavy, but the normal usage of the web application should be quicker, as you’re only fetching the relevant data to fill each of the required widgets.

This does sound resource intensive, for computers back in the 1990s, but today’s iPad, iPhone, and modern laptop computers should be powerful enough to handle it.

Or did I just describe some already popular JavaScript framework?

Post reply on HN