Some mundane things I learnt about JavaScript recently.

Interesting things about JavaScript learned through real-life experience.

JavaScript is a welcoming language – admittedly, one that takes much longer to master than it does to learn.

At work, I’ve been working on interesting bits and pieces which are helping me solidify my knowledge. Here are some interesting as well as mundane things I’ve gotten comfortable with recently.

window.print() is not a part of ECMAScript

Documentation

I was surprised to learn that window.print() is not a spec – only a Living Standard. From a cursory test, it seems Firefox for Android does not know what to do with window.print() whereas Chrome for Android does. If you wrap your website in an Android WebView or UIWebView/WKWebView, neither Android nor iOS will get you anywhere with a call to this function.

Android’s developer documentation specifically states:

You cannot use JavaScript in a HTML document to trigger printing.

Object spread syntax to write flexible constructors and methods

Documentation

Consider:

const createShape = (config) => {
return {
length: 50,
breadth: 30,
{...config}
};
};

let sqauare = createShape({ breadth: 50 });
let cube = createShape({ height: 75 });

You can add additional key-value pairs, or overrwrite an existing pair, or both!

It was a tweet by @wesbos that really solidified it as a pattern in my memory, despite having used it at work several times.

Your browser can find out a lot about your battery

Your browser lets anyone query what the status and charge level of your battery is. How cool and frightening is that?

Firefox's console with output from JavaScript Navigator's getBattery function call.

An interesting Atom keyboard shortcut

I love keyboard shortcuts – easing them into your mental map and muscle memory is a huge plus to your day-to-day productivity, without the stress of remembering 20 keyboard combinations all at once.

I chanced upon Ctrl + R which generates symbols and lets you look through or search just the methods in a file. This is what it looks like in a React file using ES6 classes:

Dropdown menu inside Atom, showing shortcuts to symbols in the currently open file.
0

Comment via email.