Skip to content

Instantly share code, notes, and snippets.

@ugultopu
ugultopu / initialize-js-project.sh
Last active October 13, 2020 19:29
The right way to initialize a JavaScript (NodeJS) project
mkdir project-name
cd project-name
git init
yarn init
@ugultopu
ugultopu / simple-custom-event-in-nodejs.js
Created October 18, 2020 18:14
An example that demonstrates how to create a custom event in NodeJS
// Credits: https://youtu.be/ENrzD9HAZK4?t=403
const { EventEmitter } = require('events');
const eventEmitter = new EventEmitter();
eventEmitter.on('lunch', () => {
console.log('yum 🍣');
});
eventEmitter.emit('lunch');
@ugultopu
ugultopu / You don't need to use http.createServer.md
Last active October 19, 2020 18:53
Simply using the Server constructor function is enough

AFAICT, [createServer][http] function is being kept only for compatibility, because all it does now is to simply call the Server constructor as is:

In [http] module:

function createServer(opts, requestListener) {
  return new Server(opts, requestListener);
}

Hence, we can simply use:

@ugultopu
ugultopu / Return value comparison in JavaScript constructors.md
Last active October 22, 2020 16:20
Observing what we obtain when we call a function using the "new" operator, when the function explicitly returns a value

Code:

// Undefined return value.
function A() {
  this.name = 'Name';
  return;
}

// Reference to instance.
@ugultopu
ugultopu / Using ES modules in browser.html
Created October 25, 2020 00:33
npm packages written with ES modules can be used in browsers without extra set up
<!DOCTYPE html>
<html>
<head>
<script type="module">
// Note that it has to be "lodash-es". Just "lodash" does not work
// because just "lodash" uses CommonJS modules (that is, Node.JS
// modules), which are not implemented in (web) browsers.
import cloneDeep from 'https://cdn.jsdelivr.net/npm/lodash-es@4.17.15/cloneDeep.js';
function log(object) {
@ugultopu
ugultopu / The right way of embedding gists on a web page.md
Created October 28, 2020 19:24
A better approach to generating a static web-page with your gists on the back-end, instead of dynamically generating them on the front-end

GitHub provides us an easy way of embedding gists on a web page. When we navigate to a gist on GitHub, there is a drop-down list element with the default option named "Embed". The value of this is an HTML <script> tag. The src attribute of this <script> tag is nothing but simply the GitHub Gists URL of this gist, with .js appended to it. When we navigate to this URL (the one that ends with .js), we observe that this is a JavaScript source file with two document.write statements. The first document.write statement adds an HTML <link> element for the CSS of the embedded gist. The second document.write is the gist itself.

Embedding gists this way is acceptable for one or two gists (although still being a bad approach) but for more gists, it simply results in a polluted HTML and needless HTTP GET requests for each gist embed link (the HTTP GET requests to the relevant JavaScript files). In other words, every visit to your web page will result in multiple (as many as the number of gists you have

@ugultopu
ugultopu / Print all prototypes.js
Last active October 30, 2020 03:08
Prints all (enumerable and non-enumerable) properties of an object by traversing the prototype chain
function getAllProperties(object) {
// Same as:
// if (object === null || object === undefined) return;
if (object == null) return;
const properties = {...Object.getOwnPropertyDescriptors(object)};
let currentObject = properties;
while (object = Object.getPrototypeOf(object)) {
currentObject.PROTO = Object.getOwnPropertyDescriptors(object);
currentObject = currentObject.PROTO;
}
@ugultopu
ugultopu / Context comparison for functions defined within constructor functions.md
Last active November 1, 2020 05:35
Observe how does "this" propagates to the inner functions

Code:

function usesThis(name) {
  this.myName = name;

  function returnMe() {
      return this;        //scope is lost because of the inner function
  }

A backup of this question and answer in case somebody decides to delete it.

Question

Why can't I initialize the imported property without first assigning it to a variable?

The following code works fine:

@ugultopu
ugultopu / How does Node.js source code have "magic" identifiers?.md
Last active December 3, 2020 22:39
An insight into how can non-declared or non-imported identifiers can be used in Node.js source code

This commit is a good starting point. The commit is about changing a commonly used function (which is named internalBinding) into a "magic identifier". Apparently, it has something to do with something called NativeModule.