Reason behind this self invoking anonymous function variant

By default, invoking a function like (function(){/*…*/})() will set the value of this in the function to window (in a browser) irrespective of whatever the value of this may be in the enclosing context where the function was created. Using call allows you to manually set the value of this to whatever you want. In … Read more

How to handle circular dependencies with RequireJS/AMD?

This is indeed a restriction in the AMD format. You could use exports, and that problem goes away. I find exports to be ugly, but it is how regular CommonJS modules solve the problem: define(“Employee”, [“exports”, “Company”], function(exports, Company) { function Employee(name) { this.name = name; this.company = new Company.Company(name + “‘s own company”); }; … Read more

How to stop babel from transpiling ‘this’ to ‘undefined’ (and inserting “use strict”)

For Babel >= 7.x ES6 code has two processing modes: “script” – When you load a file via a <script>, or any other standard ES5 way of loading a file “module” – When a file is processed as an ES6 module In Babel 7.x, files are parsed as “module” by default. The thing that is … Read more

Difference between “module.exports” and “exports” in the CommonJs Module System

module is a plain JavaScript object with an exports property. exports is a plain JavaScript variable that happens to be set to module.exports. At the end of your file, node.js will basically ‘return’ module.exports to the require function. A simplified way to view a JS file in Node could be this: var module = { … Read more

Relation between CommonJS, AMD and RequireJS?

RequireJS implements the AMD API (source). CommonJS is a way of defining modules with the help of an exports object, that defines the module contents. Simply put, a CommonJS implementation might work like this: // someModule.js exports.doSomething = function() { return “foo”; }; //otherModule.js var someModule = require(‘someModule’); // in the vein of node exports.doSomethingElse … Read more

tech