Created
March 2, 2016 00:08
-
-
Save cantremember/bb24a99c9abc6376d7d3 to your computer and use it in GitHub Desktop.
exposing @Protected methods in an ES6 module to ES5
This file contains hidden or 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
/* | |
consumer.js | |
`node consumer.js` | |
*/ | |
import Lib from './lib'; | |
const { method: libMethod } = Lib; // <= 2-step destructure | |
console.log(Lib.method()); | |
console.log(libMethod()); | |
import { method as altMethod, _PROTECTED } from './alt'; | |
console.log(altMethod()); | |
console.log(_PROTECTED._method()); | |
// Bacon / ES5 | |
var Lib2 = require('./lib').default; // <= ouch | |
console.log(Lib2.method()); | |
var Alt2 = require('./alt'); | |
console.log(Alt2.method()); | |
/* | |
lib.js | |
*/ | |
function _private() { | |
return 'LIB'; | |
} | |
const METHODS = { | |
method() { | |
return METHODS._protected(); | |
}, | |
_protected() { | |
return _private(); | |
}, | |
}; | |
export default METHODS; | |
/* | |
alt.js | |
*/ | |
function _private() { | |
return 'ALT'; | |
} | |
export const _PROTECTED = { | |
_method() { | |
return _private(); | |
}, | |
}; | |
export function method() { | |
return _PROTECTED._method(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment