Created
October 9, 2018 11:24
-
-
Save balkhaev/95aa2981bb7e69fa7b109a462d0ddb89 to your computer and use it in GitHub Desktop.
JS Module Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Abstract | |
const privateSingletonVariable = {}; | |
export const publicSingletonVariable = []; | |
export function publicPureFunction(arg1, arg2) { | |
return arg1 - arg2; | |
} | |
export default function constructorModule(key, settings = {}) { | |
const privateOption1 = settings.option1 || 'default option 1'; | |
const privateOption2 = settings.option2 || 2; | |
function privateMethod(additionalOption) { | |
return privateOption1 + additionalOption; | |
} | |
publicSingletonVariable.push(key); | |
privateSingletonVariable[key] = {}; | |
return { | |
publicApiMethod(arg1, arg2) { | |
console.log(arg1, arg2); | |
}, | |
publicApiMethodUsePrivate(arg1) { | |
return privateMethod(arg1); | |
}, | |
publicApiMethodUsePure(arg1) { | |
return publicPureFunction(privateOption2, arg1); | |
} | |
} | |
} | |
// Real World - Preloader | |
const nodePreloaders = []; | |
export const addNode = (node, type) => nodePreloaders.push({ node, type }); | |
export const findNodeIndex = node => nodePreloaders.findIndex(nodeLoader => nodeLoader.node === node); | |
export const existNode = node => findNodeIndex(node) > -1; | |
export const removeNode = node => { | |
const nodeIndex = findNodeIndex(node); | |
if (nodeIndex > -1) { | |
nodePreloaders.splice(nodeIndex, 1); | |
} | |
}; | |
export const getNodes = () => nodePreloaders; | |
const preloaderTypes = { | |
mini(node) { | |
// code for mini preloader | |
}, | |
midi(node) { | |
// code for mini preloader | |
}, | |
large(node) { | |
// code for mini preloader | |
}, | |
} | |
export function preloader(node, type) { | |
// Check for null and undefined | |
if (preloaderTypes[type] == null) { | |
throw Error('Loader type is undefined!'); | |
} | |
if (existNode(node)) { | |
return; | |
} | |
addNode(node, type); | |
preloaderTypes[type](node); | |
return { | |
unBlock() { | |
unblock(node); | |
}, | |
isBlocked() { | |
return existNode(node); | |
} | |
} | |
} | |
export function unblock(node) { | |
if (!existNode(node)) { | |
return; | |
} | |
removeNode(node); | |
} | |
export default { | |
unblock: unblock, | |
block(node, opts) { | |
return preloader(node, 'midi', opts); | |
}, | |
blockMini(node, opts) { | |
return preloader(node, 'mini', opts); | |
}, | |
blockMidi(node, opts) { | |
return preloader(node, 'midi', opts); | |
}, | |
blockLarge(node, opts) { | |
return preloader(node, 'large', opts); | |
}, | |
isBlocked(node) { | |
return existNode(node); | |
}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.moduleName = ((win, doc, $) => { | |
const privateVariable = doc.innerHeight; | |
function privateMethod() { | |
return privateVariable; | |
} | |
return { | |
publicMethod() { | |
return typeof $ !== 'undefined'; | |
} | |
} | |
})(window, document, jQuery) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment