Javascript Training 2026

From bibbleWiki
Jump to navigation Jump to search

Introduction

Never thought I would be doing this. Love typescript and this will be a challenge but doing more with less is fun too. So on here will be the bits I never knew about JS

Node

commonJS module.exports = require. And although import works for a function, it does not work for a class so you have to use it
ESM export = import

So never made a package outside of using npm so this was a surprise. Make a folder fun_m and add package.json

{
  "name" : "myModule",
  "main" : "./stimpy.js"
}

You put your file stimpy.js in the same directory

module.exports = {
  speak: function() {
    return "happy, happy, joy, joy";
  }
};

So in your main you can import the folder

const fun = require('./fun_m');

console.log(fun.speak());

arguments

Well this was a stunner. First what does this return

function add(x, y) {
  return(x + y);
}
console.log(add(1, 2, 3, 4, 10, 20));

It ignores the extra arguments and print 3

function args () {
  return arguments;
}
console.log(args(8, 7, 6, 5, 4));

Well turns out arguments is a keyword in js and it prints [8, 7, 6, 5, 4]

Anonymous Functions

Never really got this until today. Knew what it did but just accepted it worked

console.log((function (x, y) { return x + y })(3, 4));

Looks easier to read like this

a = function (x, y) { return x + y }
b = (a)(3, 4)

console.log(b);

In fact u can lose the brackets around a

a = function (x, y) { return x + y }
b = a(3, 4)

console.log(b);