Blog
What is AMD, CommonJS
AMD (Asynchronous Module Definition) The AMD module format itself is a proposal for defining modules where both the module and dependencies can be asynchronously loaded.
“define” method for facilitating module definition and a “require” method for handling dependency loading.
define('myModule',
['foo', 'bar'],
// module definition function
// dependencies (foo and bar) are mapped to function parameters
function ( foo, bar ) {
// return a value that defines the module export
// (i.e the functionality we want to expose for consumption)
// create your module here
var myModule = {
doStuff:function(){
console.log('Yay! Stuff');
}
}
return myModule;
});require(['foo', 'bar'], function ( foo, bar ) {
// rest of your code here
foo.doSomething();
});CommonJS (CJS)
CJS module is a reusable piece of JavaScript which exports specific objects made available to any dependent code - there are typically no function wrappers around such modules.
// package/lib is a dependency we require
var lib = require('package/lib');
// some behaviour for our module
function foo(){
lib.log('hello world!');
}
// export (expose) foo to other modules
exports.foo = foo;// an application consuming 'foobar'
// access the module relative to the path
// where both usage and module files exist
// in the same directory
var foobar = require('./foobar').foobar,
test = new foobar();
test.bar(); // 'Hello bar'References
- https://addyosmani.com/writing-modular-js/
- https://requirejs.org/docs/whyamd.html#purposes
What does not not operator do (!!) ?
It converts Object to boolean. If it was falsey (e.g. 0, null, undefined, etc.), it will be false, otherwise, true.
References:
- https://dev.to/macmacky/70-javascript-interview-questions-5gfi#16-what-does-the-operator-do
- https://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript
Difference between function declaration and function expression
var result = add(10, 20);
console.log(result); // 30
// Function declaration
function add(num1, num2) {
return num1 + num2;
}
// Function expression
var add = function (num1, num2) {
return num1 + num2;
};Function declarations are hoisted to the top of the code by the browser before any code is executed. Specifically, all of the functions written with function declarations are “known” before any code is run. This allows you to call a function before you declare.
Function expressions, however, do not hoist. If you try to run a function before you’ve expressed it, you’ll get an error.
References
Difference between Pure and Function React JS component
React.PureComponent is similar to React.Component. The difference between them is that React.Component doesn’t implement shouldComponentUpdate(), but React.PureComponent implements it with a shallow prop and state comparison.
Function React component is that which accepts a single “props” (which stands for properties) object argument with data and returns a React element.
References:
- https://reactjs.org/docs/react-api.html#reactpurecomponent
- https://reactjs.org/docs/components-and-props.html
What are Curry functions in JavaScript
Currying is a technique of evaluating function with multiple arguments, into sequence of function with single argument. That is, when we turn a function call add(1,2,3) into curriedAdd(1)(2)(3) .
function curriedAdd(x) {
return function(y) {
return function(z) {
return x + y + z;
};
}
}
curriedAdd(1)(2)(3) // result would be 6
let curriedAdd_1_2 = curriedAdd(1)(2);
curriedAdd_1_2(3) // result would be 6
curriedAdd_1_2(10) // result would be 13References